Intersoft ClientUI 8 > ClientUI Fundamentals > Commanding Overview > Commanding How-to Topics > How-to: Explicitly Set the Command Target |
This example shows how to setup a command source to execute a command on an explicit command target.
The command target is the element on which the command is executed. With regards to a RoutedCommand, the command target is the element at which routing of the Executed and CanExecute starts. If the CommandTarget is set on an ICommandSource and the corresponding command is not a RoutedCommand, the command target is ignored.
The command source can explicitly set the command target. If the command target is not defined, the element with keyboard focus will be used as the command target. One of the benefits of using the element with keyboard focus as the command target is that it allows the application developer to use the same command source to invoke a command on multiple targets without having to keep track of the command target. For example, if a UXMenuItem invokes the Paste command in an application that has a UXTextBox control and a UXPasswordBox control, the target can be either the UXTextBox or UXPasswordBox depending on which control has keyboard focus. To learn more about commanding, see Commanding Overview.
The following example shows how to explicitly set the command target in markup and in code behind.
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.KeyBinding" Title="CommandOverview Page" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot"> <Intersoft:CommandManager.CommandBindings> <Intersoft:CommandBindingCollection> <Intersoft:CommandBinding Command="Commands:EditingCommands.Copy" CanExecute="CopyCmdCanExecute" Executed="CopyExecuted"/> </Intersoft:CommandBindingCollection> </Intersoft:CommandManager.CommandBindings> <Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> <Intersoft:UXMenuBar Name="menuBar1" Intersoft:DockPanel.Dock="Top"> <Intersoft:UXMenuItem Header="Edit"> <Intersoft:UXMenuItem Name="mnu_Copy" Header="Copy" Command="Commands:EditingCommands.Copy" CommandTarget="{Binding ElementName=textBox1}" /> </Intersoft:UXMenuItem> </Intersoft:UXMenuBar> <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 KeyBinding : UXPage { public CommandOverview() { EditingCommands.Initialize(); InitializeComponent(); } private void CopyCmdCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void CopyExecuted(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.