Floating FB sharing byReview Results

Floating FB sharing byReview Results

  • WPF Tutorial

    • # Read in Your Language

    Styling WPF applications

    Tutorial Home | Styles,Skins & Templates | Commands| Triggers

    Commands in WPF

    WPF Commands :

    Command Model

    It delegates events to the appropriate commands.

    - It keeps the enabled state of a control synchronized with the state of the corresponding command.

    Commands. A command represents an application task and keeps track of whether it can be executed. (But does not contain code);

    Command bindings. Each command binding links a command to the related application logic, for a particular area of your user interface.

    Command sources. A command source triggers a command. For example, a MenuItem and a Button can both be command sources. Clicking them executes the bound command.

    Command targets. A command target is the element on which the command is being performed.

    The ICommand Interface :

    The heart of the WPF command model is the System.Windows.Input.ICommand interface, which defines how commands work.

    This interface includes two methods and an event:

            public interface ICommand
            void Execute(object parameter);
            bool CanExecute(object parameter);
            event EventHandler CanExecuteChanged;

    Execute() method would contain the application task logic
    (for example, printing the document).

    The CanExecute() method returns the state of the command: true if it’s enabled and false if it’s disabled.

    Both Execute() and CanExecute() accept an additional parameter object that we can use to pass along any extra information you need.

    CanExecuteChanged event is raised when the state changes.

    This is a signal to any controls using the command that they should call the CanExecute() method to check the command’s state.

    The RoutedCommand Class

    System.Windows.Input.RoutedCommand class implements ICommand .

    All WPF commands are instances of RoutedCommand.

    The RoutedCommand class adds extra infrastructure for event tunneling and bubbling.

            public void Execute(object parameter, IInputElement target)
    public bool CanExecute(object parameter, IInputElement target) {...}

    The RoutedUICommand Class

    RoutedUICommand class derived from RoutedCommand.

    RoutedUICommand is intended for commands with text that is displayed somewhere in the user interface (for example, the text of a menu item or the tooltip for a toolbar button).

    The RoutedUICommand class adds a single property—Text—which is the display text for that command.


    This class provides the common commands, including clipboard commands (such as Copy, Cut, and Paste) and document commands (such as New, Open, Save, SaveAs, Print, and so on).


    This class provides commands used for navigation, including some that are designed for page-based applications (such as BrowseBack, BrowseForward, and NextPage).


    This class provides a document-editing commands, including commands for moving around (MoveToLineEnd, MoveLeftByWord, MoveUpByPage, and so on).


    This class includes a set of commands for dealing with multimedia (such as Play, Pause, NextTrack, and IncreaseVolume).

    Commands :

    Programmatic control :

            // Create the binding.
            CommandBinding binding = new CommandBinding(ApplicationCommands.New);
            // Attach the event handler.
            binding.Executed += NewCommand_Executed;
            // Register the binding.
            private void NewCommand_Executed(object sender, 
            ExecutedRoutedEventArgs e)
            MessageBox.Show("New command triggered by " + e.Source.ToString());

            <Window x:Class="Commands.TestNewCommand"
            <CommandBinding Command="ApplicationCommands.New"
            <StackPanel Margin="5">
            <Button Padding="5" Command="ApplicationCommands.New">New</Button>

    Using Commands :

            <MenuItem Header="File">
            <MenuItem Command="New"></MenuItem>
            <Button Command="New" Content="{x:Static ApplicationCommands.New}">

    Built-In Commands :

            <Button Command="Cut"
            CommandTarget="{Binding ElementName=txtDocument}">Cut</Button>
            <Button Command="Copy"
            CommandTarget="{Binding ElementName=txtDocument}">Copy</Button>
            <Button Command="Paste"
            CommandTarget="{Binding ElementName=txtDocument}">Paste</Button>

    Custom Commands :

            public class DataCommands<
            private static RoutedUICommand requery;
            static DataCommands()
            // Initialize the command.
            InputGestureCollection inputs = new InputGestureCollection();
            inputs.Add(new KeyGesture(Key.R, ModifierKeys.Control, "Ctrl+R"));
            requery = new RoutedUICommand(
            "Requery", "Requery", typeof(DataCommands), inputs);
            public static RoutedUICommand Requery
            get { return requery; }
    After defining custom command, use it

            <CommandBinding Command="local:DataCommands.Requery"
            <Button Margin="5" Command="local:DataCommands.Requery">Requery

    To complete this example, you simply need to implement the RequeryCommand_Executed() event handler in your code.

    << Previous >> | << Next >>