Intersoft ClientUI Documentation
How-to: Create CommandBinding using Code

This example shows how to implement a RoutedCommand by creating the CommandBinding 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 shows how to create the command binding and assign it to a UIElement using code.

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.CommandBindingCode" 
        Title="CommandOverview Page"
        d:DesignWidth="640" d:DesignHeight="480">

        <Grid x:Name="LayoutRoot">

            <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>
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 CommandBindingCode : UXPage
    {
        public CommandBindingCode()
        {
            // It is important to initialize the static EditingCommands class
            // before InitializeComponent is called
            EditingCommands.Initialize();
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(CommandBindingCode_Loaded);
        }

        private void CommandBindingCode_Loaded(object sender, RoutedEventArgs e)
        {
            // Create a CommandBinding and attaching an Executed and CanExecute handler
            CommandBinding cutCommandBinding = new CommandBinding(
                EditingCommands.Cut,
                CutExecuted,
                CutCmdCanExecute);

            // Create the CommandBindingCollection to hold the command binding
            CommandBindingCollection bindingCollection = new CommandBindingCollection();
            bindingCollection.Add(cutCommandBinding);

            // Set the binding collection to the layout root
            CommandManager.SetCommandBindings(this.LayoutRoot, bindingCollection);
        }

        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

Concepts

Other Resources