我有很多ListView,每个都绑定到自己的ListCollectionView,每个都有相同的ContextMenu需求。我不想重复相同的ContextMenu N次,所以我在参考资料中定义它并通过StaticResource引用它。
当右键单击ListView中的项目X并单击MenuItem时,如何在代码隐藏中访问对象X?
<Window.Resources>
<ContextMenu x:Key="CommonContextMenu">
<MenuItem Header="Do Stuff" Click="DoStuff_Click" />
</ContextMenu>
</Window.Resources>
<ListView ItemsSource="{Binding Path=ListCollectionView1}" ContextMenu="{StaticResource CommonContextMenu}">
...
</ListView>
<ListView ItemsSource="{Binding Path=ListCollectionView2}" ContextMenu="{StaticResource CommonContextMenu}">
...
</ListView>
private void DoStuff_Click(object sender, RoutedEventArgs e)
{
// how do i get the selected item of the right listview?
}
更新
感谢Michael Gunter的回答,我现在使用以下扩展方法:
public static ListView GetListView(this MenuItem menuItem)
{
if (menuItem == null)
return null;
var contextMenu = menuItem.Parent as ContextMenu;
if (contextMenu == null)
return null;
var listViewItem = contextMenu.PlacementTarget as ListViewItem;
if (listViewItem == null)
return null;
return listViewItem.GetListView();
}
public static ListView GetListView(this ListViewItem item)
{
for (DependencyObject i = item; i != null; i = VisualTreeHelper.GetParent(i))
{
var listView = i as ListView;
if (listView != null)
return listView;
}
return null;
}
答案 0 :(得分:1)
1)将上下文菜单放在每个ListView
内的每个项目上,而不是放在每个ListView
本身上。这样可以避免在单击ListView
中的空白区域时弹出上下文菜单。为此,请使用ListView.ItemContainerStyle
属性。 (如果你真的想要ListView
本身的上下文菜单,请告诉我,我会相应地编辑这个答案。)
<Window.Resources>
<ContextMenu x:Key="CommonContextMenu">
<MenuItem Header="Do Stuff" Click="DoStuff_Click" />
</ContextMenu>
<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource CommonContextMenu}" />
</Style>
</Window.Resources>
<ListView ItemsSource="{Binding Path=ListCollectionView1}" ItemContainerStyle="{StaticResource ListViewItemStyle}">
...
</ListView>
<ListView ItemsSource="{Binding Path=ListCollectionView2}" ItemContainerStyle="{StaticResource ListViewItemStyle}">
...
</ListView>
2)使用以下代码确定右键单击的项目。
private void DoStuff_Click(object sender, RoutedEventArgs e)
{
var menuItem = sender as MenuItem;
if (menuItem == null)
return;
var contextMenu = menuItem.Parent as ContextMenu;
if (contextMenu == null)
return;
var listViewItem = contextMenu.PlacementTarget as ListViewItem;
if (listViewItem == null)
return;
var listView = GetListView(listViewItem);
if (listView == null)
return;
// do stuff here
}
private ListView GetListView(ListViewItem item)
{
for (DependencyObject i = item; i != null; i = VisualTreeHelper.GetParent(i))
{
var listView = i as ListView;
if (listView != null)
return listView;
}
return null;
}