duminică, 25 aprilie 2010




  • Essentially, commands are a means by which shared actions can be grouped and invoked in several different ways within an application.
  • It is a more abstract and loosely-coupled version of events.
  • We can associate a MenuItem or a Button with a particular command
    • We are making a clearer statement of the intended behavior
  • Commands are considered "routed" in part because they follow the same line of tunneling and bubbling as events do in WPF
  • Commands usually target the element with the keyboard focus
  • There are five concepts at the heart of the command system:
    • Command object
      • An object identifying a particular command, such as copy or paste
    • Input binding
      • An association between a particular input (e.g., Ctrl-C) and a command (e.g., Copy)
    • Command source
      • The object that invoked the command, such as a Button, or an input binding
    • Command target
      • The UI element that will be asked to execute the command—typically the control that had the keyboard focus when the command was invoked
    • Command binding
      • A declaration that a particular UI element knows how to handle a particular command

  • InputBinding ib = new InputBinding(ApplicationCommands.Properties, new KeyGesture(Key.Enter, ModifierKeys.Alt));

    CommandBinding cb = new CommandBinding(ApplicationCommands.Properties);
    cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);

  • InputBinding is used to bind input gestures.
  • The full syntax for a command attribute in XAML is:
    • If only the event name is present, the event is presumed to be one of the standard ones
  • WPF defines a number of built-in commands
    • All of the built-in command objects use a class called RoutedUICommand
    • The target of the RoutedUICommand is determined by the way in which the command was invoked
    • Typically, the target will be whichever element currently has the focus, unless the command source’s CommandTarget has been set
    • If a RoutedUICommand fails to find a command binding, it checks to see whether the initial target was in a nested focus scope.
  • Commands have automatic support for input gestures (such as keyboard shortcuts).
  • Some of WPF’s controls have built-in behavior tied to various commands
  • A fundamental difference between commands and events is that commands do not exist in relation to controls
  • When the RoutedCommand class is invoked by your application, it raises the PreviewExecuteEvent and the ExecuteEvent events
  • ApplicationCommands, ComponentCommands, EditCommands, MediaCommands
    • TextBox controls utilize valuable ApplicationCommands class functionalities such as cut, copy, paste, and so on.
  • Commands are invoked using the notion of input gestures. Two input gesture types are supported: MouseGesture and KeyGesture
    • The InputGestureCollection class is used to house all the input gestures that can invoke a command.
  • A command is any object implementing the ICommand interface
    • Execute—The method that executes the command-specific logic
    • CanExecute—A method returning true if the command is enabled or false if it is disabled
    • CanExecuteChanged—An event that is raised whenever the value of CanExecute changes
  • Button, CheckBox, and MenuItem have logic to interact with any command on your behalf
  • WPF’s built-in commands are exposed as static properties of five different classes:
    • ApplicationCommands—Close, Copy, Cut, Delete, Find, Help, New, Open, Paste, Print, PrintPreview, Properties, Redo, Replace, Save, SaveAs, SelectAll, Stop, Undo, and more
    • ComponentCommands—MoveDown, MoveLeft, MoveRight, MoveUp, ScrollByLine, ScrollPageDown, ScrollPageLeft, ScrollPageRight, ScrollPageUp, SelectToEnd, SelectToHome, SelectToPageDown, SelectToPageUp, and more
    • MediaCommands—ChannelDown, ChannelUp, DecreaseVolume, FastForward, IncreaseVolume, MuteVolume, NextTrack, Pause, Play, PreviousTrack, Record, Rewind, Select, Stop, and more
    • NavigationCommands—BrowseBack, BrowseForward, BrowseHome, BrowseStop, Favorites, FirstPage, GoToPage, LastPage, NextPage, PreviousPage, Refresh, Search, Zoom, and more
    • EditingCommands—AlignCenter, AlignJustify, AlignLeft, AlignRight, CorrectSpellingError, DecreaseFontSize, DecreaseIndentation, EnterLineBreak, EnterParagraphBreak, IgnoreSpellingError, IncreaseFontSize, IncreaseIndentation, MoveDownByLine, MoveDownByPage, MoveDownByParagraph, MoveLeftByCharacter, MoveLeftByWord, MoveRightByCharacter, MoveRightByWord, and more
  • All RoutedUICommand objects define a Text property containing a name for the command that’s appropriate to show in a user interface
  • To plug in custom logic, you need to add a CommandBinding to the element that will execute the command or any parent element
    • A CommandBinding class is used to provide UI element-specific context to the execution of commands within a window or area of an application.
      • offers a total of four events: Execute, PreviewExecute, QueryEnabled, and PreviewQueryEnabled.
      • CommandBinding objects can be used to determine whether a particular command is currently enabled
      • Command bindings rely on the bubbling nature of command routing
  • FocusManager.IsFocusScope="True"
  • CommandTarget="{Binding ElementName=targetControl}"
  • Sample of binding a command from xaml file:
    <CommandBinding Command="Help" CanExecute="HelpCanExecute" Executed="HelpExecuted"/>

Sample of using commands from code:

m_buttonHelp.Command = ApplicationCommands.Help;
m_buttonHelp.Content = ApplicationCommands.Help.Text;
CommandBindings.Add(new CommandBinding(ApplicationCommands.Help, HelpExecuted, HelpCanExecute));

private void HelpCanExecute(object i_sender, CanExecuteRoutedEventArgs i_e)
//to enable/disable a command
i_e.CanExecute = true;

private void HelpExecuted(object i_sender, ExecutedRoutedEventArgs i_e)

  • Commands such as Help define a default input gesture that executes the command (if you press F! from keyboard, Help action will take place)
    • How to change the default gesture:
      <KeyBinding Command="Help" Gesture="CTRL+H"/>
      <KeyBinding Command="NotACommand" Key="F1"/>

Command Source

  • The command source is the object that was used to invoke the command
  • It might be a user interface element, such as a button, hyperlink, or menu item
  • Command sources implement the ICommandSource interface

  • public interface ICommandSource
    ICommand Command { get; }
    object CommandParameter { get; }
    IInputElement CommandTarget { get; }

  • If you set the Command property to a command object, the source will invoke this command when clicked, or in the case of an input gesture, when the user performs the
relevant gesture.
  • The CommandParameter property allows us to pass information to a command when it is invoked

Niciun comentariu:

Trimiteți un comentariu