私は多くの ListViews を持っており、それぞれが独自の ListCollectionView にバインドされており、それぞれが同じ ContextMenu を必要としています。同じ ContextMenu を N 回繰り返したくないので、Resources で定義し、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;
}