none
ContextMenu MenuItem Command Binding RRS feed

  • Question

  • I've been trying to get a menu item in a context menu to activate using a command. The XAML is this.

        <Window.ContextMenu>
            <ContextMenu BorderBrush="#FF000000" Background="#FF333333" Foreground="#FFFF8000" Padding="0,0,0,0">
                <MenuItem Header="New Node" Command="{Binding Path=NewNodeCommand, ElementName=Main_Window, Mode=Default}"/>
            </ContextMenu>
        </Window.ContextMenu>

    My command never seems to get hit from a context menu but if I bind that same ocmmand to a button or menu item which is not in a context menu.. Something like this:

    <Window
        <Grid x:Name="LayoutRoot">
            <Button Margin="0,8,8,0" Content="New Node" HorizontalAlignment="Right" VerticalAlignment="Top" Width="92" Height="24" Command="{Binding Path=NewNodeCommand, ElementName=Main_Window, Mode=Default}">
            </Button>
        </Grid>
    </Window>

    The command gets hit and everything works.

    The only way i've found to make this work correctly is to use the Click on the button and call a the method directly. Thats all fine except that It would be nice to not have to have multiple code paths to access the same method.

    Cheers

    Kyle
    Saturday, October 27, 2007 5:36 PM

Answers

  • Thanks for the input unfortunatly neither of those methods seem to work for me. The one which i've found successful is this
    <MenuItem Command="{Binding Path=PlacementTarget.NewNodeCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
    Sunday, October 28, 2007 3:26 PM
  • Hi Kylawl, 


           If you define the NewNodeCommand as a static property exposed by Window1, then the following   XAML snippet should work:
               

    Code Block

              <ContextMenu>

                  <MenuItem Header="Paste" Command="{x:Static local:Window1.MyCommand}"/>

              </ContextMenu>

     

     

        Hope this helps

     

    Thursday, November 1, 2007 1:53 AM

All replies



  • The problem is the binding that you are using in the menu item.
    {Binding Path=NewNodeCommand, ElementName=Main_Window, Mode=Default}

    This binding cannot be applied for a menu item inside a contexst menu since a context menu is not part of the element tree. To confirm this look in the output window of Visual studio and when you run your app you should see Binding error bla bla bla.
    there is a similar post that describes this problem

    What you need to do to fix this problem is basically change the binding to not use the ElementName (this only applies for when using the context menu, but it will work for the button that you have in the XAML)....

    An idea whould be to do the following

    {Binding Path=NewNodeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window} } }

    Hope it helps
    Saturday, October 27, 2007 5:46 PM
  • Even with the relative source it does not seem to find the element. I've even specified my specific class type with no positive results.

    I have also tried increasing the AncestorLevel. How does it find the ancestor? Is there a parent property?

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpressionStick out tongueath=NewNodeCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

    Cheers

    Kyle
    Saturday, October 27, 2007 7:08 PM
  • You shouldn't need a Binding to specify the Command.  If you have a RoutedCommand defined in the Window1 class, you should simply be able to do the following in your markup:

     

    Code Block

     

    <MenuItem Command="src:Window1.NewNodeCommand" Header="New Node" />

     

     

     

    Where src is an xmlns alias to your namespace.  For example:

     

    Code Block

     

    <Window x:Class="WindowsApplication1.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:src="clr-namespace:WindowsApplication1"

        Title="WindowsApplication1" Height="300" Width="300">

     

     

     

    Sunday, October 28, 2007 12:07 AM
  • Hi,

     

    Dr.WPF has a good point! You should not need binding for a command....

     

    Regards

    Sunday, October 28, 2007 9:16 AM
  • Thanks for the input unfortunatly neither of those methods seem to work for me. The one which i've found successful is this
    <MenuItem Command="{Binding Path=PlacementTarget.NewNodeCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
    Sunday, October 28, 2007 3:26 PM
  • Hi Kylawl, 


           If you define the NewNodeCommand as a static property exposed by Window1, then the following   XAML snippet should work:
               

    Code Block

              <ContextMenu>

                  <MenuItem Header="Paste" Command="{x:Static local:Window1.MyCommand}"/>

              </ContextMenu>

     

     

        Hope this helps

     

    Thursday, November 1, 2007 1:53 AM
  • however, what if I need to pass a parameter from ElementName to the command?
    Sunday, December 7, 2008 2:27 AM
    • Proposed as answer by sirios210 Thursday, August 8, 2013 1:36 PM
    Tuesday, April 17, 2012 9:02 AM