#1,092 – An Example of Using a CommandTarget

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"
        Title="Commands" Width="320" Height="220">

        <TextBox Name="txtSomeText"
                 Width="220" Height="25" Margin="10"/>
        <Button Content="Paste"
                CommandTarget="{Binding ElementName=txtSomeText}"
                Margin="10" Padding="10,3"
                HorizontalAlignment="Center" />