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:
Hello,

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 :
XML
<Style x:Key="BlockContainerStyle" TargetType="{x:Type ContentControl}" >
   ....
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu ItemsSource="{Binding Path=MenuItems}" ItemContainerStyle="{StaticResource ContextMenuItemStyle}"/>
            </Setter.Value>
        </Setter>
    </Style>

....
XML
<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>
    </Style>


..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
Posted

1 solution

Hi Beda,

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

http://joshsmithonwpf.wordpress.com/2008/07/22/enable-elementname-bindings-with-elementspy/[^]


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

Cheers
   

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