Goal: Right click on ListBox and get a binded contextMenu.
public class MyViewModel
{
public List<string> ContextMenuItems{ get; set; }
public ItemObject MyObject{ get; set; }
}
public class ItemObject{
public List<OtherObjects> SomeCollection{ get; set; }
}
Now my ListBox is Binded to "SomeCollection", but my ContextMenu should access a Binding outside the listbox's Binding. I have tried and cannot get it working at all, my context menu is always empty. Any idea why? This is in UserControl and not a Window, not that it is relevant. I am just pointing out why my AncestorType points to a UserControl
<ListBox ItemsSource="{Binding SomeCollection}">
<ListBox.ContextMenu >
<ContextMenu DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" ItemsSource="{Binding ContextMenuItems}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding}" Command="{Binding MyCommand}"/>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>