WPF using custom RoutedUICommands or simple event handlers?

Andy Dent picture Andy Dent · Feb 17, 2009 · Viewed 8.8k times · Source

I was talking to someone today about picking a design pattern for how to handle logic in their WPF program and hoping that the SO community can help with further advice to make the decision easier. What factors in favour of commands outweigh the inconvenience?

I prepared a full sample along with some UML diagrams of the first two of three approaches:

  1. Use Click event handlers on buttons and menus.
  2. Use commands bound in XAML.
  3. Use commands bound in code, with the XAML kept for pure GUI layout and styling.

The introductory course he'd been on and many of the books show simple Click event handlers as the natural way to connect logic to UI objects.

He was a bit stunned by the amount of overhead required to use commands with both the command being created in the code behind file:

public static readonly ICommand cmdShow2 = new RoutedUICommand(
  "Show Window2", "cmdShow2", 
  typeof(TestDespatchWindow));

and then even more code in the XAML with the wordy way the command has to be identified and bound:

<Window x:Class="WPFDispatchDemo.TestDespatchWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:w="clr-namespace:WPFDispatchDemo"..>

    <Window.CommandBindings>
      <CommandBinding Command="{x:Static w:TestDespatchWindow.cmdShow2}"
        Executed="OnShow2" />
    </Window.CommandBindings>
      <DockPanel>
        <StackPanel Margin="0,8,0,0">
          <Button x:Name="Show2EventBased" 
                  Margin="10,2,10,2" 
                  Click="OnShow2" 
                  Content="Show2 via WPF Event"/>
          <Button x:Name="Show2Command" 
                  Command="{x:Static w:TestDespatchWindow.cmdShow2}"
                  Margin="10,2,10,2" 
                  Content="Show2 via WPF"/>
        </StackPanel>
    </DockPanel>
  </Window>

I can't (yet) claim to be a WPF expert so I may have painted things as more complex than they really are but my suspicion is that you can't simplify things much more than the above.

Edit:

I found an interesting 3-way comparison between DelegateCommand, RoutedCommand and Event.

Answer

Nir picture Nir · Feb 18, 2009

Commands their advantages and disadvantages, you have to choose based on your situation, I highly recommend you make that choice on a case basis, don't choose "the one true way" for the entire project.

For some cases the separation between sender and receiver and the ability to send commands using only XAML is a big advantage (for a good example look how the ScrollBar control template communicates with the control logic at http://msdn.microsoft.com/en-us/library/ms742173.aspx ).

In other cases commands can turn what would have been a 2 lines event handler into some impossible to follow monstrosity involving changing 4 separate places in the application (How should the ViewModel close the form? ).