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>
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);
}
我在DataContext中得到null。如何获取已调用此MenuFlyout的ListViewItem或此ListViewItem的内容?
答案 0 :(得分:0)
如何获取已调用此MenuFlyout的ListViewItem或此ListViewItem的内容?
当您尝试触发属于MenuFlyout
ContextFlyout
的{{1}}时,您可能需要右键单击PC上的ListView
或长按它在移动设备上,两者都会首先触发RightTapped
事件句柄。通过ListView
,您可以获取当前权限的ListViewItemPresenter
,然后可以获得RightTappedRoutedEventArgs
。例如:
XAML
DataContext
背后的代码
<ListView
x:Name="Lessons"
IsItemClickEnabled="True"
RightTapped="Lessons_RightTapped"
SelectionMode="None" />