0

XAML:

<Page.Resources>
    <MenuFlyout x:Key = "ElementMenuFlyout">
        <MenuFlyoutItem Text = "Edit" Icon = "Edit" Click = "Edit"/>
    </MenuFlyout>
</Page.Resources>

...
<ListView SelectionMode = "None" IsItemClickEnabled = "True" x:Name="Lessons"></ListView>

Initialization of ListView.Items:

foreach (Tables.Lesson lesson in lessons)
{
    Lessons.Items.Add(new ListViewItem {Content = lesson, ContextFlyout = this.Resources["ElementMenuFlyout"] as MenuFlyout });
}

MenuFlyoutItem.Click:

private void Edit(object sender, RoutedEventArgs e)
{
    Frame.Navigate(typeof(ElementInfo), (e.OriginalSource as MenuFlyoutItem).DataContext);
}

I get null in DataContext. How to get ListViewItem that have called this MenuFlyout or Content of this ListViewItem?

krlzlx
  • 5,752
  • 14
  • 47
  • 55

1 Answers1

0

How to get ListViewItem that have called this MenuFlyout or Content of this ListViewItem?

When you tried to trigger the MenuFlyout which is belong to the ContextFlyout of ListView, you may need to right click the ListView on the PC or long tap it on the mobile, both will trigger the RightTapped event handle firstly. By RightTappedRoutedEventArgs you can get the current right tapped ListViewItemPresenter then can get the DataContext. For example:

XAML

<ListView
    x:Name="Lessons"
    IsItemClickEnabled="True"
    RightTapped="Lessons_RightTapped"
    SelectionMode="None" />

Code behind

private ListViewItemPresenter currentitem; 
private void Edit(object sender, RoutedEventArgs e)
{        
    if (currentitem != null)
    {  
       Frame.Navigate(typeof(ElementInfo), currentitem.DataContext);
    }  
} 
private void Lessons_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
    currentitem = e.OriginalSource as ListViewItemPresenter;
}
Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21