C# MenuFlyoutItem获取父项
我在ListView项目上附加了一个弹出按钮,简化如下:C# MenuFlyoutItem获取父项,c#,windows-phone-8.1,flyout,C#,Windows Phone 8.1,Flyout,我在ListView项目上附加了一个弹出按钮,简化如下: <ListView.ItemTemplate> <DataTemplate> <StackPanel IsHoldingEnabled="True" Holding="ListView_Holding" > <FlyoutBase.AttachedFlyout> <MenuFlyout>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel IsHoldingEnabled="True" Holding="ListView_Holding" >
<FlyoutBase.AttachedFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="remove" Click="MenuFlyoutItem_Click" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
private void ListView_Holding(object sender, HoldingRoutedEventArgs e)
{
FrameworkElement senderElement = sender as FrameworkElement;
FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
flyoutBase.ShowAt(senderElement);
}
private void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
{
}
私有无效列表视图(对象发送方、HoldingRoutedEventArgs e)
{
FrameworkElement senderElement=发送方作为FrameworkElement;
FlyoutBase FlyoutBase=FlyoutBase.GetAttachedFlyout(senderElement);
飞出基地。ShowAt(senderElement);
}
私有无效菜单使用项单击(对象发送者、路由目标)
{
}
如何从菜单项“附加”到的MenuFlyoutItem\u单击中获取ListView项?我试过一些方法,但都没能成功
如果需要,我可以发布更多代码。您可以通过获取DataContext来获得确切的项目。这将是您绑定到的集合中的对象
var datacontext = senderElement.DataContext;
然后可以从该DataContext获取ListViewItem
ListViewItem item = this.NameOfYourList.ContainerFromItem(datacontext) as ListViewItem;
谢谢你的回复,一些我不知道的有用的东西。由于我想在用户单击弹出按钮上的“删除”时执行代码从列表视图中删除项目,而不是长按项目本身,因此如何从MenuFlyoutItem_Click
?@blawford数据上下文是项目本身(不是列表视图项目,而是确切的上下文项目)。如果要将ListView.ItemsSource绑定到ObservableCollection,只需从ListView中删除此datacontext,就会立即反映在ListView中!