I am having some trouble figuring out how to set the correct DataContext
on a ContextMenu
.
I have a collection of view models who are the source of an ItemsControl
. Each view model has a collection of items which are also the source of another ItemsControl
. Each item is used to draw image which has a ContextMenu
. The MenuItems
in that ContextMenu
need to bind to a command on the view model, but the PlacementTarget
of the ContextMenu
is pointing to the individual item.
My Xaml looks something like this:
<ItemsControl ItemsSource="{Binding Markers"}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image>
<Image.ContextMenu>
<ContextMenu>
<MenuItem Header="Edit" Command="{Binding EditCommand}" />
</ContextMenu>
</Image.ContextMenu>
</Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
How can I set the DataContext
of the ContextMenu
to the item's corresponding parent view model?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…