Intersoft ClientUI 8 > ClientUI Fundamentals > Commanding Overview > Commanding How-to Topics > How-to: Add a Command to a UXMenuItem |
This example shows how to configure a UXMenuItem as the command source for a Copy command. For more information on commanding, see Commanding Overview.
Several user interface controls in ClientUI such as UXMenuItem, UXButton and UXHyperlinkButton, implements ICommandSource interface. Two properties that ICommandSource expose are Command and CommandTarget. Command is the command that will be invoked and CommandTarget is the element where event routing will start when the command is invoked. If CommandTarget is not defined, the element that has keyboard focus will be the set as the target.
The class implementing ICommandSource defines what it means for the command to be invoked. UXMenuItem and UXButton define the Click event as the means to invoke the command. If the command cannot be executed on the particular CommandTarget, the UXMenuItem will be disabled. When the command can execute on the CommandTarget, the UXMenuItem will be enabled.
In addition, ClientUI controls that support commanding also provides an option to customize the user interface behavior when a command cannot be executed. For more information, see Commanding Overview.
The following example shows how to configure a UXMenuItem as the command source for a Copy command by assigning the command to the Command property of the UXMenuItem, then define the CommandBinding, and write the event handler to implement the logic for the command.
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_Cut" Header="Cut" Command="Commands:EditingCommands.Cut" Icon="../Images/CutHS.png"/> <Intersoft:UXMenuItem Name="mnu_Copy" Header="Copy" Command="Commands:EditingCommands.Copy" Icon="../Images/CopyHS.png"/> <Intersoft:UXMenuItem Name="mnu_Paste" Header="Paste" Command="Commands:EditingCommands.Paste" Icon="../Images/PasteHS.png"/> </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.