How to add custom routed command in WPF?

I have an application containing a menu and submenu. I attached application commands to some submenu items such as Cut, Copy, and Paste.
I also have some other menu items in which there are no application commands.
How can I add a custom command binding to these submenu items?
I looked through this article but could not connect the event to my submenu items.

+41
c # wpf
Mar 02 '09 at 5:52
source share
3 answers

I use the static class that I place after the Window1 class (or whatever the window class is called), where I instantiate the RoutedUICommand class:

public static class Command { public static readonly RoutedUICommand DoSomething = new RoutedUICommand("Do something", "DoSomething", typeof(Window1)); public static readonly RoutedUICommand SomeOtherAction = new RoutedUICommand("Some other action", "SomeOtherAction", typeof(Window1)); public static readonly RoutedUICommand MoreDeeds = new RoutedUICommand("More deeds", "MoreDeeeds", typeof(Window1)); } 

Add a namespace to the window layout using the namespace that contains the Window1 class:

 xmlns:w="clr-namespace:NameSpaceOfTheApplication" 

Now I can create bindings for commands in the same way as for application commands:

 <Window.CommandBindings> <CommandBinding Command="ApplicationCommands.Open" Executed="CommandBinding_Open" /> <CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Paste" /> <CommandBinding Command="w:Command.DoSomething" Executed="CommandBinding_DoSomething" /> <CommandBinding Command="w:Command.SomeOtherAction" Executed="CommandBinding_SomeOtherAction" /> <CommandBinding Command="w:Command.MoreDeeds" Executed="CommandBinding_MoreDeeds" /> </Window.CommandBindings> 

And use the bindings in the menu, for example:

 <MenuItem Name="Menu_DoSomething" Header="Do Something" Command="w:Command.DoSomething" /> 
+86
Mar 02 '09 at 6:10
source share
— -

Instead of defining them in a static class, you can also declare commands directly in XAML. Example (adapted from a good Huff example):

 <Window.Resources> <RoutedUICommand x:Key="DoSomethingCommand" Text="Do Something" /> <RoutedUICommand x:Key="DoSomethingElseCommand" Text="Do Something Else" /> </Window.Resources> <Window.CommandBindings> <CommandBinding Command="{StaticResource DoSomethingCommand}" Executed="CommandBinding_DoSomething" /> <CommandBinding Command="{StaticResource DoSomethingElseCommand}" Executed="CommandBinding_DoSomethingElse" /> </Window.CommandBindings> ... <MenuItem Name="Menu_DoSomething" Header="Do Something" Command="{StaticResource DoSomethingCommand}" /> 
+61
Jun 15 '10 at 15:38
source share

I know my answer is too late , but I hope this helps the future .

I like the answers of Guff and Heinzi, but you can only use one command to achieve the previous result. I usually use the help command

  <Window.CommandBindings> <CommandBinding Command="{StaticResource Help}" Executed="HelpExecuted" /> </Window.CommandBindings> 

and I use CommandParametr with every call e.g.

 <Window.InputBindings> <KeyBinding Command="{StaticResource Help}" Key="A" Modifiers="Ctrl" CommandParameter="Case1"/> <KeyBinding Command="{StaticResource Help}" Key="B" Modifiers="Ctrl" CommandParameter="Case2"/> <KeyBinding Command="{StaticResource Help}" Key="C" Modifiers="Ctrl" CommandParameter="Case3"/> <KeyBinding Command="{StaticResource Help}" Key="D" Modifiers="Ctrl" CommandParameter="Case4"/> <MouseBinding Command="{StaticResource Help}" MouseAction="LeftDoubleClick" CommandParameter="Case5" /> </Window.InputBindings> 

or

 <Button Command="Help" CommandParameter="Case6" Content="Button"> <Button.InputBindings> <KeyBinding Command="{StaticResource Help}" Gesture="Ctrl+D" CommandParameter="Case7"/> </Button.InputBindings> </Button> 

and in cs file

 private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) { string str = e.Parameter as string; switch (str) { case null://F1 Pressed default Help //Code break; case "Case1": //Code break; case "Case2": //Code break; case "Case3": //Code break; case "Case4": break; case "Case5": //Code break; case "Case6": //Code break; case "Case7": //Code break; } e.Handled = true; } 

and if you use the MVVM pattern

 private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) { string str = e.Parameter as string; Mvvm_Variable.Action(Input: str); e.Handled = true; } 

and move the switch to the ViewModule website. and Action is a method in the same ViewModule class.

+15
Aug 31 2018-11-11T00:
source share



All Articles