Many commands provide binding "built in". For example, All it takes to implement Cut and Paste on a pair of Textboxes is this code:
<Grid>
<ToolBar VerticalAlignment="Top">
<Button Command="ApplicationCommands.Cut">Cut</Button>
<Button Command="ApplicationCommands.Paste">Paste</Button>
</ToolBar>
<TextBox Name="TextBox1" Margin="0,0,0,150" Height="30" />
<TextBox Name="TextBox2" Margin="0,0,0,70" Height="30"/>
</Grid>
The illustration shows this simple use of Cut and Paste in action. But to prove it to yourself, you can also Cut from one of the Textboxes and Paste into, for example, Notepad. Notice that WPF even takes care of disabling and enabling the Button controls as required. More specifically, to enable both Cut and Paste, the CanExecute event is handled by the TextBox that has the focus and CanExecute is set to True. If something is in the Clipboard already, Paste is enabled as soon as a Textbox get the focus.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The binding is done for you by WPF in this case, but in other cases, you will have to understand how to code a WPF binding. To learn more about binding, you might want to check out the About Visual Basic article, Introducing Data Binding in WPF and XAML. This isn't data binding, it's command binding, but the way it's done is very similar.
To illustrate how to bind a command, lets look at formatting text in a RichTextBox as italic.
If you simply add a RichTextBox control to a WPF window, you can make text inside it bold or italic by using the gestures Ctrl-B or Ctrl-I with no code at all. Or, you can add XAML code to create the content as part of a FlowDocument object. (For more on FlowDocuments, you can try the About Visual Basic article, FlowDocument - A New Kind of Object in WPF.)
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
WPF includes the code to accomplish the same thing as the XAML code in the RichTextBox control. But an ordinary TextBox doesn't have this code.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
What we want to do is associate this same built-in logic with a Button control that we supply. To do this, we have to bind the button's command logic to the RichTextBox control. The essential XAML attributes to do this are:
<Button
Name="ItalicButton"
Command="EditingCommands.ToggleItalic"
CommandTarget="{Binding ElementName=RichTextBox1}">
Italic
</Button>
<RichTextBox
Name="RichTextBox1" />
(The positioning attributes, Margin, Width and so forth, aren't shown to keep the example simple.)
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

