Understanding the Role of Control Commands

Windows Presentation Foundation provides support for what might be considered ‘control agnostic events’
via control commands. As you know, a CLR .NET event type is defined within a specific base class and can
only be used by that class or a derivative thereof. Normal CLR events are tightly coupled to the class in
which they are defined.

In contrast, WPF control commands are event-like entities that are independent from a specific control and, in
many cases, can be successfully applied to numerous and seeming unrelated control types. For example, WPF
supports Copy, Paste, and Cut commands. They can be applied to a wide variety of UI elements (menu
items, toolbar buttons, custom controls), as well as keyboard shortcuts (Ctrl+C, Ctrl+V, and so on).   

Although other UI toolkits (such as Windows Forms) provided standard events for such purposes, the end
result was, typically, redundant and hard-to-maintain code. Under the WPF model, commands can be used as
an alternative. The end result typically yields a smaller and more flexible code base. A WPF control command
is any object that supports a property (often called Command) that returns an object implementing the
ICommand interface.


// C# (VB code would be similar)
public interface ICommand
{
// Occurs when changes occur that affect whether
// the command should execute.
event EventHandler CanExecuteChanged;

// Defines the method that determines whether the command
// can execute in its current state.
  
bool CanExecute(object parameter);

// Defines the method to be called when the command is invoked.   
void Execute(object parameter);
}


WPF provides numerous command objects out of the box. These static classes define numerous properties
that expose objects that implement
ICommand.







































Consider the following XAML description of a menu system. Notice that copy, paste, cut commands are
connected to various menu items. This is literally all you need to do to enable this behavior. You will check
this out firsthand in your next lab.


<Menu DockPanel.Dock = "Top"
HorizontalAlignment = "Left" Background = "White"
   BorderBrush = "Black">
<MenuItem Header = "_File" Click = "FileExit_Click" >
<Separator/>
<MenuItem Header = "_Exit" MouseEnter = "MouseEnterExitArea"
   MouseLeave = "MouseLeaveArea" Click = "FileExit_Click"/>
</MenuItem>

<!-- New menu item with commands. -->  
<MenuItem Header = "_Edit">
  <MenuItem
Command = "ApplicationCommands.Copy"/>
  <MenuItem
Command = "ApplicationCommands.Cut"/>
  <MenuItem
Command = "ApplicationCommands.Paste"/>
</MenuItem>

<MenuItem Header = "_Tools">
<MenuItem Header = "_Spelling Hints"
    MouseEnter = "MouseEnterToolsHintsArea"
  MouseLeave = "MouseLeaveArea" Click = "ToolsSpellingHints_Click"/>
</MenuItem>
</Menu>
Control Commands
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials


WPF Control Command
Object


Example Control Command      
 Properties


Meaning in Life


ApplicationCommands

Close, Copy, Cut, Delete,
Find, Open, Paste, Save,
SaveAll, Redo, Undo

Defines properties that
represent ‘application-level’
commands.


ComponentsCommands

MoveDown, MoveFocusBack
MoveLeft, MoveRight,
ScrollToEnd ScrollToHome

Defines properties that map
to common commands
performed by UI elements.


MediaCommands

BoostBase, ChannelUp
ChannelDown, FastForward
NextTrack, Play, Rewind,
Select, Stop

Defines properties that allow
various media-centric
controls to issue common
commands.


NavigationCommands

BrowseBack, BrowsForward
Favorites, LastPage
NextPage, Zoom

Defines numerous properties
that are used for the
applications that utilize the
WPF navigation model.



EditingCommands

AlignCenter,    
CorrectSpellingError,
DecreaseFontSize,
EnterLineBreak,
EnterParagraphBreak,
MoveDownByLine,
MoveRightByWord

Defines numerous properties
typically used when
programming with objects
exposed by the WPF  
document API.
Services