How to configure "ContextMenu" in ListView for Windows Phone 8.1?

I have a problem with MenuFlyout. I am trying to get a context menu that works well to give the user the delete and edit options. But if the user clicks on one of these options, there seems to be no decision on how to get the list or the selected item. Maybe I just embarrassed something, but I searched all day, and although people had similar problems, none of the solutions worked for me.

Xaml

<Pivot x:Name="MyPivot" Title="MyTitle" ItemsSource="{Binding}"> <Pivot.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding Title}"/> </DataTemplate> </Pivot.HeaderTemplate> <Pivot.ItemTemplate> <DataTemplate> <ScrollViewer> <ListView x:Name="MyListView" ItemsSource="{Binding Items}"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="Margin" Value="0,0,0,10"/> </Style> </ListView.ItemContainerStyle> <ListView.ItemTemplate> <DataTemplate> <Grid Holding="Grid_Holding"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <FlyoutBase.AttachedFlyout> <MenuFlyout> <MenuFlyoutItem x:Name="EditButton" Text="Edit" Click="EditButton_Click"/> <MenuFlyoutItem x:Name="DeleteButton" Text="Delete" Click="DeleteButton_Click"/> </MenuFlyout> </FlyoutBase.AttachedFlyout> // Content (TextBlocks...) </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> </ScrollViewer> </DataTemplate> </Pivot.ItemTemplate> </Pivot> 

FROM#

  private void Grid_Holding(object sender, HoldingRoutedEventArgs e) { FrameworkElement senderElement = sender as FrameworkElement; FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement); flyoutBase.ShowAt(senderElement); } 
+7
c # windows xaml
source share
1 answer

Once the click event is raised, you can get the DataContext of the FrameworkElement element.

 private void EditButton_Click(object sender, RoutedEventArgs e) { var datacontext = (e.OriginalSource as FrameworkElement).DataContext; //this datacontext is probably some object of some type T (whatever is in your Items collections you haven't specified in your question) } 
+8
source share

All Articles