Reputation: 792
I'm trying to allow users of this class to define the menu context items, as in different use cases we'll need to expose different context menu items.
Source XAML
<Grid>
<DataGrid>
<DataGrid.Columns>
...
</DataGrid.Columns>
<DataGrid.ContextMenu>
<ContextMenu
Visibility="{Binding SelectedItem, Converter={SomeConverter}}">
</ContextMenu>
</DataGrid.ContextMenu>
</DataGrid>
</Grid>
How I'd like to use it:
<x:MyCollectionControl>
<MenuItems>
<MenuItem Header="Do Something"
Visibility="Collapsed" />
<MenuItem Header="Do Something Else"
Visibility="{Binding SomeCondition}" />
</MenuItems>
</x:MyCollectionControl>
What do I need to add to the control or view model to expose the context menu items so they can be defined where I'm using the control?
Upvotes: 0
Views: 25
Reputation: 792
Of course, I worked out a fix about 5 minutes after posting this.
Added the following to my control code.
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ItemCollection MenuItems => MyCollectionDataGrid.ContextMenu?.Items;
And used this to reference the control.
<trade1:MyCollectionControl x:Name="SomeDataGrid">
<trade1:MyCollectionControl.MenuItems>
<MenuItem Header="Do Something"
Visibility="Collapsed" />
<MenuItem Header="Do Something Else"
Visibility="{Binding SomeCondition}" />
</trade1:MyCollectionControl.MenuItems>
</trade1:MyCollectionControl>
Upvotes: 0
Reputation: 516
You could have the ContextMenu's ItemsSource binded to a collection property in your ViewModel.
Assuming all your ViewModels inherit from a certain Interface(your own) they could implement a getter to this collection and if the have items or not(for disabling the ContextMenu).
For anything else you will have to be more specific.
Upvotes: 1