WPF Concepts—Controls with Built-In Command Bindings
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| .NET Articles |
| © 2007 Sams Publishing |
Controls with Built-In Command Bindings
It can seem almost magical when you encounter it, but some controls in WPF contain their own command bindings. The simplest example of this is the TextBox control, which has its own built-in bindings for the Cut, Copy, and Paste commands that interact with the clipboard, as well as Undo and Redo commands. This not only means that TextBox responds to the standard Ctrl+X, Ctrl+C, Ctrl+V, Ctrl+Z, and Ctrl+Y keyboard shortcuts, but that it's easy for additional elements to participate in these actions.
The following standalone XAML demonstrates the power of these built-in command bindings:
<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Orientation="Horizontal" Height="25"> <Button Command="Cut" CommandTarget="{Binding ElementName=textBox}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/> <Button Command="Copy" CommandTarget="{Binding ElementName=textBox}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/> <Button Command="Paste" CommandTarget="{Binding ElementName=textBox}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/> <Button Command="Undo" CommandTarget="{Binding ElementName=textBox}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/> <Button Command="Redo" CommandTarget="{Binding ElementName=textBox}" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/> <TextBox x:Name="textBox" Width="200"/> </StackPanel>
You can paste this content into XamlPad or save it as a .xaml file to view in Internet Explorer, because no procedural code is necessary. Each of the five Buttons is associated with one of the commands and sets its Content to the string returned by each command's Text property. The only new thing here is the setting of each Button's CommandTarget property to the instance of the TextBox (again using data binding functionality discussed in Chapter 9). This causes the command to be executed from the TextBox rather than the Button, which is necessary in order for it to react to the commands.
This XAML produces the result in Figure 3.8. The first two Buttons are automatically disabled when no text in the TextBox is selected, and automatically enabled when there is a selection. Similarly, the Paste Button is automatically enabled whenever there is text content on the clipboard, or disabled otherwise.
![]()
Figure 3.8 The five Buttons work as expected without any procedural code, thanks to TextBox's built-in bindings.
Button and TextBox have no direct knowledge of each other, yet though commands they can achieve rich interaction. This is why WPF's long list of built-in commands is so important. The more that third-party controls standardize on WPF's built-in commands, the more seamless (and declarative) interaction can be achieved among controls that have no direct knowledge of each other.
|

