#1,092 – An Example of Using a CommandTarget
June 12, 2014 1 Comment
Setting the CommandTarget property allows an element executing a routed command to initiate the routing of a command with a different element.
In the example below, clicking on the Button initiates a Paste command, but the routed command originates with a TextBox rather than the button. In this particular case, because a TextBox implicitly has a command binding for the Paste command, text currently in the paste buffer is automatically pasted into the TextBox. No code-behind is required in order for this to work.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Commands" Width="320" Height="220"> <StackPanel> <TextBox Name="txtSomeText" Width="220" Height="25" Margin="10"/> <Button Content="Paste" Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=txtSomeText}" Margin="10" Padding="10,3" HorizontalAlignment="Center" /> </StackPanel> </Window>
Pingback: Dew Drop – June 12, 2014 (#1796) | Morning Dew