Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom WPF command pattern example

I've done some WPF programing and one thing I never got was the command pattern. Every example seems to be for built in ones, edit, cut, paste. Anyone have an example or suggestion of best practice for custom commands?

like image 212
Brian Leahy Avatar asked Aug 12 '08 04:08

Brian Leahy


2 Answers

Ah ha! A question I can answer! Firstly, I should mention that I have personally found it easier to define and hook up commands in code rather than in XAML. It allows me to hook up the handlers for the commands a little more flexibly than an all XAML approach does.

You should work out what commands you want to have and what they relate to. In my application, I currently have a class for defining important application commands like so:

public static class CommandBank {   /// Command definition for Closing a window   public static RoutedUICommand CloseWindow { get; private set; }    /// Static private constructor, sets up all application wide commands.   static CommandBank()   {     CloseWindow = new RoutedUICommand();     CloseWindow.InputGestures.Add(new KeyGesture(Key.F4, ModifierKeys.Alt));     // ...   } 

Now, because I wanted to keep the code all together, using a code only approach to Commands lets me put the following methods in the class above:

/// Closes the window provided as a parameter public static void CloseWindowExecute(object sender, ExecutedRoutedEventArgs e) {   ((Window)e.Parameter).Close(); }  /// Allows a Command to execute if the CommandParameter is not a null value public static void CanExecuteIfParameterIsNotNull(object sender, CanExecuteRoutedEventArgs e) {   e.CanExecute = e.Parameter != null;   e.Handled = true; } 

The second method there can even be shared with other Commands without me having to repeat it all over the place.

Once you have defined the commands like this, you can add them to any piece of UI. In the following, once the Window has Loaded, I add command bindings to both the Window and MenuItem and then add an input binding to the Window using a loop to do this for all command bindings. The parameter that is passed is the Window its self so the code above knows what Window to try and close.

public partial class SimpleWindow : Window {   private void WindowLoaded(object sender, RoutedEventArgs e)   {     // ...     this.CommandBindings.Add(       new CommandBinding(         CommandBank.CloseWindow,         CommandBank.CloseWindowExecute,         CommandBank.CanExecuteIfParameterIsNotNull));      foreach (CommandBinding binding in this.CommandBindings)     {        RoutedCommand command = (RoutedCommand)binding.Command;        if (command.InputGestures.Count > 0)        {          foreach (InputGesture gesture in command.InputGestures)          {            var iBind = new InputBinding(command, gesture);            iBind.CommandParameter = this;            this.InputBindings.Add(iBind);          }        }     }      // menuItemExit is defined in XAML     menuItemExit.Command = CommandBank.CloseWindow;     menuItemExit.CommandParameter = this;     // ...   }    // .... } 

I then also later have event handlers for the WindowClosing and WindowClosed events, I do recommend you make the actual implementation of commands as small and generic as possible. As in this case, I didn't try to put code that tries to stop the Window closing if there is unsaved data, I kept that code firmly inside the WindowClosing event.

Let me know if you have any follow up questions. :)

like image 186
Nidonocu Avatar answered Sep 27 '22 23:09

Nidonocu


I blogged about a bunch of resources on WPF Commands along with an example last year at http://blogs.vertigo.com/personal/alanl/Blog/archive/2007/05/31/commands-in-wpf.aspx

Pasting here:

Adam Nathan’s sample chapter on Important New Concepts in WPF: Commands

MSDN article: The Command Pattern In WPF

Keyvan Nayyeri: How to Add Commands to Custom WPF Control

Ian Griffiths: Avalon Input, Commands, and Handlers

Wikipedia: Command Pattern

MSDN Library: Commanding Overview

MSDN Library: CommandBinding Class

MSDN Library: Input and Commands How-to Topics

MSDN Library: EditingCommands Class

MSDN Library: MediaCommands Class

MSDN Library: ApplicationCommands Class

MSDN Library: NavigationCommands Class

MSDN Library: ComponentCommands Class

Also buried in the WPF SDK samples, there's a nice sample on RichTextBox editing which I've extended. You can find it here: RichTextEditor.zip

like image 28
Alan Le Avatar answered Sep 27 '22 23:09

Alan Le