I am trying to bind commands to various MenuItems
in a ContextMenu
that I will be hooking up with a Button
. But for this, I am defining all commands as static
in a class that I have imported in my ResourceDictionary
.
public class DesignerCanvas{
....
public static RoutedCommand MyCommand = new RoutedCommand();
....
}
and in my MainWindow.xaml
, I am hooking this command with my implementations in MainWindow.xaml.cs
as:
<CommandBinding Command="{x:Static Designer:DesignerCanvas.MyCommand}"
Executed="DoStuff"
CanExecute="CanDoStuff" />
And in my ResourceDictionary.xaml
, I am having a Button
that I am hooking the ContextMenu
with using Triggers
:
<Button x:Name="btnMyButton" Content="Click this">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="ContextMenu.IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem x:Name="myMenu" Header="MyMenuItem 1">
<MenuItem x:Name="menuItem1" Header="MySubMenuItem 1"
Command="{x:Static DesignerItems:DesignerCanvas.MyCommand}"> <<<=== Command Binding
<MenuItem.Icon>
<Image Source="myImage.png" Width="20"/>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
But this doesn't seem to be working since the menu item that has a Command
specified in the XAML is being shown as disabled
and also neither of the CanDoStuff()
and DoStuff()
are getting hit by the debugger. Also, since i am NOT using a ViewModel for this, I am UNABLE TO write something like:
<MenuItem Command="{Binding Path=somePathInViewModel, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" />
How can I do this, any help would be greatly appreciated. Thanks in advance.