Intersoft ClientUI Documentation
How-to: Implement a RoutedCommand

This example shows how to implement a RoutedCommand by assigning the routed command to the Command property of the control, defining the CommandBinding in XAML, and create the event handlers using code.

Example

Description

A routed command is typically implemented through these processes: a RoutedCommand is initially associated to a control that implements ICommandSource such as UXButton, create a CommandBinding, and then create the event handlers which implement the logic for the RoutedCommand.

The following code example illustrates the big picture of these processes and shows how the routed command and command binding can be easily defined in XAML.

Code

XAML
Copy Code
<Intersoft:UXPage 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:Intersoft="http://intersoft.clientui.com/schemas"
    xmlns:Commands="clr-namespace:ClientUIApplication_Docs.Commands"
        mc:Ignorable="d"
        x:Class="ClientUIApplication_Docs.RoutedCommands.CommandOverview" 
        Title="CommandOverview Page"
        d:DesignWidth="640" d:DesignHeight="480">

        <Grid x:Name="LayoutRoot">

            <Intersoft:CommandManager.CommandBindings>
                <Intersoft:CommandBindingCollection>
                    <Intersoft:CommandBinding Command="Commands:EditingCommands.Cut"
                                              CanExecute="CutCmdCanExecute"
                                              Executed="CutExecuted"/>
                </Intersoft:CommandBindingCollection>
            </Intersoft:CommandManager.CommandBindings>

            <Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom">
                <Intersoft:UXToolBar Name="toolBar1" Intersoft:DockPanel.Dock="Top">
                    <Intersoft:UXToolBarButton Name="btn_Cut" Command="Commands:EditingCommands.Cut" DisplayMode="Image" Icon="../Images/CutHS.png" ToolTipService.ToolTip="Cut"/>
                </Intersoft:UXToolBar>
                <Intersoft:UXTextBox Name="textBox1" Text="Type your text here..." Intersoft:DockPanel.IsFillElement="True" />
            </Intersoft:DockPanel>

        </Grid>
<Intersoft:UXPage>

The following code example shows the event handler for the CanExecute and Executed routed event for the CommandBinding associated to the Cut command.

C#
Copy Code
using System.Windows;
using ClientUIApplication_Docs.Commands;
using Intersoft.Client.Framework.Input;
using Intersoft.Client.UI.Navigation;

namespace ClientUIApplication_Docs.RoutedCommands
{
    public partial class CommandOverview : UXPage
    {
        public CommandOverview()
        {
            // It is important to initialize the static EditingCommands class
            // before InitializeComponent is called
            EditingCommands.Initialize();
            InitializeComponent();
        }

        private void CutCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CutExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            string command = ((RoutedCommand)e.Command).Name;
            string target = ((FrameworkElement)e.OriginalSource).Name;

            MessageBox.Show("The " + command +  " command has been invoked on target object " + target);
        }
    }
}

To learn how to create the routed commands for EditingCommands used in the above example, see How-to: Create Routed Commands In a Static Class.

See Also

Other Resources

Concepts