Intersoft ClientUI 8 > ClientUI Fundamentals > Commanding Overview > Commanding How-to Topics > How-to: Use Class Binding to Implement a RoutedCommand |
This example shows how to use class command binding API to register a routed command on a specific type rather than an instance.
In addition to instance command binding, the commanding framework in ClientUI also supports class command binding API which is available in the CommandManager class. The class command binding is particularly useful when you have a fairly large amount of commands to bind to the controls.
The following code example shows how to use class command binding API to register a routed command on a specific type rather than an instance.
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.ClassCommandBinding" 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 ClassCommandBinding : UXPage { public ClassCommandBinding() { // It is important to initialize the static EditingCommands class // before InitializeComponent is called EditingCommands.Initialize(); InitializeComponent(); } static ClassCommandBinding() { RoutedUICommand[] commands = new RoutedUICommand[] { EditingCommands.Cut, EditingCommands.Copy, EditingCommands.Paste }; // register class-level command binding for RoutedUICommand foreach (RoutedUICommand command in commands) { CommandManager.RegisterClassCommandBinding( typeof(UXPage), new CommandBinding(command, OnCommandExecuted, OnCanExecute)); } } private static void OnCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private static void OnCommandExecuted(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.