Custom RoutedUI Commands In WPF


Creating A WPF Project

Fire up Visual Studio 2008, create a WPF Application, and name it as CustomCommandRoutedUI.

image1.gif

Now let's create a static class in Window1.xaml.cs

image2.gif

As you see in above code display, we have created a static class with 3 static readonly commands of type RoutedUICommand.

Now to use it in XAML behind we need to add the namespace.

image3.gif

Let's have three buttons such Add, Edit and Copy. Which would operate on a ListBox item.

So let's do the design.

image4.gif

The following is the reference for the XAML.

image5.gif

Now let's have the Commands Binding for the ListBox.

image6.gif

As you see in above XAML display we have the Commands attached and we handled two events such as CanExecute and Executed for all the Commands.

Now let's set the CommandTarget and CommandParameter wherever required.

image7.gif

Now let's have the events handled.

Let's have the Add Command first.

image8.gif

Then similarly Delete Command.

image9.gif

And finally Copy Command.

image10.gif

That's it. Let's run the application and see how we managed to use CustomRoutedUI Commands.

image11.gif

image12.gif

image13.gif

Hope this article helps.