0

In trying to bind a Command to a MenuItem in my program, I've found that Commands don't work with MenuItems like they do with other controls. I've been using this post as a guide, but have had no luck so far. Basically my goal is to run a Command when the MenuItem is clicked.

This is my xaml after looking at the previously mentioned post. My Command is called CreateFiles:

<MenuItem Header="{DynamicResource save}" Command="{Binding Path=PlacementTarget.DataContext.CreateFiles, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />

My Command is created in the window's ViewModel and is declared like normal, but I will post it anyway:

private ICommand _createFiles;

public MainWindowViewModel()
{
   _createFiles = new Command(createFiles_Operations);
}

public ICommand CreateFiles { get { return _createFiles; } }
private void createFiles_Operations()
{

}

To test whether or not my Command is working I set a break point right at the first bracer. So far the program has not stopped at this break point when the MenuItem is clicked.

Since this method does not seem to work, what can I do to make Commands work with MenuItems?

Update: Command changed to ICommand

Update 2: ContextMenu & Button xaml:

<Button Click="Button_Click_1" Margin="5,4,0,0" Name="Button_1" Height="55" VerticalAlignment="Top" HorizontalAlignment="Left" Width="55" BorderBrush="Black">...

<ContextMenu x:Name="MainContextMenu" MouseLeave="ContextMenuMouseLeave" Background="White" BorderBrush="#FF959595" SnapsToDevicePixels="False">...
Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79

1 Answers1

0

You need to set Mode of RelativeSource to FindAncestor to get ContextMenu:

<MenuItem Header="{DynamicResource save}"
          Command="{Binding Path=PlacementTarget.DataContext.CreateFiles,
             RelativeSource={RelativeSource Mode=FindAncestor,
                                            AncestorType=ContextMenu}}" />

You have set PlacementRectangle property to refer to itself i.e.ContextMenu. Don't set that property, ContextMenuService internally set PlacementRectangle to the element on which it is applied (in your case it will be Button).

Remove this PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}" from ContextMenu.

It should be:

<ContextMenu x:Name="MainContextMenu"
             MouseLeave="ContextMenuMouseLeave" Background="White"
             BorderBrush="#FF959595" SnapsToDevicePixels="False">
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • I've changed my code to this. Is there any chance that there is something else that I need? My program does not stop at the break point in `createFiles_Operations()`. – Eric after dark Jan 30 '14 at 19:18
  • Return type of `CreateFiles` should be `ICommand`. What's `Command`? Are you able to see any binding error in output window? – Rohit Vats Jan 30 '14 at 19:21
  • Is Button's DataContext point to `MainWindowViewModel` correctly? – Rohit Vats Jan 30 '14 at 19:23
  • There are no binding errors in the output window. I have updated my question to show `ICommand` instead of `Command`. As far as DataContext goes, this is set in the constructor of the window like so: `DataContext = App.MainWindowViewModel = new MainWindowViewModel();`. I do not have the DataContext of the Button set separately. – Eric after dark Jan 30 '14 at 19:26
  • Only reason i could think of is Button not inheriting DataContext from window or UserControl. Otherwise code looks exactly perfect. – Rohit Vats Jan 30 '14 at 19:34
  • Aren't you setting `DataContext` on ContextMenu? – Rohit Vats Jan 30 '14 at 19:37
  • Would you like to see the xaml of my button? Why would it not be inheriting the DataContect from the window? And No I am not. – Eric after dark Jan 30 '14 at 19:38
  • I've posted the xaml for the `ContextMenu` and the `Button` that the `MenuItem` resides in. – Eric after dark Jan 30 '14 at 19:45
  • I have updated an answer. Please check. Issue is in your ContextMenu declaration. – Rohit Vats Jan 31 '14 at 05:50