Hello,
I am having trouble getting my ContextMenu to work, and I've tried all kinds of "hacks" without getting this to work.
The scenario is that I in the Ribbon menu have a ribbon Group, which has a ribbon Menu inside it which is bound to a list of viewmodels.
The menu has a DataTemplate which contains a button for each item, and each button has a ContextMenu, so that you can edit or delete the individual items.
The problem is getting the bindings set correctly. This seems to have worked at some point before, but to solve another issue which made the ContextMenu behave incorrectly (the wrong ContextMenu was shown. "Minimize the ribbon" was shown instead of the Button's ContextMenu), I tried upgrading from version 19.1 to version 22.1. After the upgrade the correct ContextMenu was shown, but the command got passed a CheckableCommandParameter instead of the "BookmarkItemViewModel".
This is basically what it looks like, with some suggested fixes being left behind:
<ribbon:Ribbon>
<ribbon:Tab Label="Home">
<ribbon:Group Label="Bookmarks">
<ribbon:Menu ItemsSource="{Binding Bookmarks.BookmarkItems}">
<ribbon:Menu.ItemTemplate>
<DataTemplate DataType="{x:Type local:BookmarkItemViewModel}">
<ribbon:Button Label="{Binding Path=Title}" Command="{Binding OpenUrlCommand}" CommandParameter="{Binding Url}" Tag="{Binding}">
<ribbon:Button.ContextMenu>
<ribbon:ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<ribbon:Button Context="MenuItem" Label="Edit" Command="{Binding EditCommand}" CommandParameter="{Binding}"/>
<ribbon:Button Context="MenuItem" Label="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding}" />
</ribbon:ContextMenu>
</ribbon:Button.ContextMenu>
</ribbon:Button>
</DataTemplate>
</ribbon:Menu.ItemTemplate>
</ribbon:Menu>
</ribbon:Group>
</ribbon:Tab>
</ribbon:Ribbon>
The ideal solution would be to bind the commands to the viewmodel which holds all the items (which the ribbon Menu is bound to), and pass the item into the command as a commandparameter.
Do you know of any simple way to bind the ContextMenu to its parent's DataContext? It doesn't have to be an exact example, but I have not been able to find anything that works. And It seems this at some point worked before, with the earlier version of Actipro controls, though I haven't been able to verify that.
[Modified 2 years ago]