Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a custom routed command in WPF?

Tags:

c#

wpf

I have an application that contains Menu and sub menus. I have attached Appliocation Commands to some of the sub menu items such as Cut, Copy and Paste.
I also have some other menu items that do not have application commands.
How could I add a custom command binding to those sub menu items?
I have gone through this article but unable to attach event to my sub menu items.

like image 996
Sachin Gaur Avatar asked Mar 02 '09 05:03

Sachin Gaur


People also ask

What are routed commands in WPF?

WPF-routed commands give you a specific mechanism for hooking up UI controls such as toolbar buttons and menu items to handlers without introducing a lot of tight coupling and repetitive code into your application.

What are routed commands?

Description. The route command allows you to make manual entries into the network routing tables. The route command distinguishes between routes to hosts and routes to networks by interpreting the network address of the Destination variable, which can be specified either by symbolic name or numeric address.

What is WPF Delegatecommand?

Delegate commands are an implementation of the System. Windows. Input. ICommand interface, so they can be used to create commands in a ViewModel. A delegate command calls methods (delegates) that you assigned to the command when the command's Execute and CanExecute logic is invoked.


2 Answers

I use a static class that I place after the Window1 class (or whatever the window class happens to be named) where I create instances of 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 in the window markup, using the namespace that the Window1 class is in:

xmlns:w="clr-namespace:NameSpaceOfTheApplication" 

Now I can create bindings for the commands just as for the 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 a menu for example:

<MenuItem Name="Menu_DoSomething" Header="Do Something" Command="w:Command.DoSomething" /> 
like image 56
Guffa Avatar answered Oct 17 '22 13:10

Guffa


Instead of defining them in a static class, you might as well declare the commands directly in XAML. Example (adapted from Guffas nice 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}" /> 
like image 22
Heinzi Avatar answered Oct 17 '22 11:10

Heinzi