Posted by: Zeeshan Amjad | February 8, 2010

Built in commands using command target

We have already seen one example of command binding here. In that example we wrote some handler to execute the built in commands. Now we are going one step further and use the built in functionality of those commands. Some WPF controls already implements some WPF commands such as TextBox implement the Application copy and paste command. Now we are going to use those feature without writing command handler.

In this program we are creating two text boxes and two buttons. First button execute the copy command and second one execute the paste command. We use the self binding to display the text property of command as a content of button. In addition we use the command target property of command binding and set first text box as a command target for copy command. Similarly we set second text box as a command target for paste command. Here is a complete XAML code of our program.

  1: <Window x:Class="WpfCommand.Window1"
  2:     xmlns=""
  3:     xmlns:x=""
  4:     Title="Command Binding" Height="300" Width="300">
  5:     <Grid Background="AliceBlue">
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition Height="2*"/>
  8:             <RowDefinition Height="2*"/>
  9:             <RowDefinition/>
 10:         </Grid.RowDefinitions>
 11:         <Grid.ColumnDefinitions>
 12:             <ColumnDefinition/>
 13:             <ColumnDefinition/>
 14:         </Grid.ColumnDefinitions>
 15:         <TextBox Name="source" Margin="5" Grid.Row="0" Grid.ColumnSpan="2"/>
 16:         <TextBox Name="destination" Margin="5" Grid.Row="1" Grid.ColumnSpan="2"/>
 17:         <Button Margin="5" Grid.Row="2" Grid.Column="0" 
 18:                 Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
 19:                 Command="ApplicationCommands.Copy" CommandTarget="{Binding ElementName=source}"/>
 20:         <Button Margin="5" Grid.Row="2" Grid.Column="1" 
 21:                 Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"
 22:                 Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=destination}"/>
 23:     </Grid>
 24: </Window>


Here we didn’t write a single line of C# code and did everything with XAML. When we click the copy button then it will copy the content of first text box in clip board and when we click the paste button then it will paste the content of clip board in second command button. Also Copy button is not unable, until we highlight some text in first text box. Here is the output of this program.



  1. […] have already seen the example of using type converter. We have already seen the usage of built in commands using command target. Now we are going to make a same program, but this time using the commands converter in C# code. […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: