Click here to Skip to main content
15,068,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am working on a project in WPF using MVVM. The main idea is that I have a design surface on wich I can add via some buttons element (like a toolbox). I want to have on each such item, a context menu with some commands. I have the ViewModel of the item I add (item has its DataContext property set to it) and I want to pass it as command parameter to menu items commands and I'm stuck because I don't know how to acces it. I'm using the folowing xaml :
<Style x:Key="BlockContainerStyle" TargetType="{x:Type ContentControl}" >
        <Setter Property="ContextMenu">
                <ContextMenu ItemsSource="{Binding Path=MenuItems}" ItemContainerStyle="{StaticResource ContextMenuItemStyle}"/>

<Style x:Key="ContextMenuItemStyle">
        <Setter Property="MenuItem.Header" Value="{Binding DisplayText}"/>
        <Setter Property="MenuItem.ItemsSource" Value="{Binding ChildItems}"/>
        <Setter Property="MenuItem.Command" Value="{Binding Command}" />
        <Setter Property="MenuItem.CommandParameter" Value="{Binding}"></Setter>

..and I want to bind the MenuItem.CommandParameter to the DataContext of the control on wich I apply the BlockContainerStyle. How should I do that ?

Thank you

1 solution

Hi Beda,

Im not sure if im putting you on the right track here,
but check this article out:[^]

This helped me out when I was struggling with a binding similar to what you are trying to do.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900