15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Comm<strong>and</strong>ing ❘ 1061<br />

Instead of using the Binding markup extension, now the binding is done as a child of the TextBox.Text<br />

element. The bound object now defines an Email property that is implemented with the simple property<br />

syntax. The UpdateSourceTrigger property defines when the source should be updated. Possible options<br />

for updating the source are:<br />

➤<br />

➤<br />

➤<br />

When the property value changes — which is every character typed by the user<br />

When the focus is lost<br />

Explicitly<br />

ValidationRules is a property of the Binding class that contains ValidationRule elements. Here<br />

the validation rule used is the custom class RegularExpressionValidationRule, where the Expression<br />

property is set to a regular expression that verifies if the input is a valid e-mail, <strong>and</strong> the<br />

ErrorMessage property that gives the error message in case the data entered to the TextBox is not valid:<br />

Email:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Comm<strong>and</strong>ing<br />

Comm<strong>and</strong>ing is a concept of WPF that creates a loose coupling between the source of an action (for<br />

example, a button) <strong>and</strong> the target that does the work (for example, a h<strong>and</strong>ler method). Events are strongly<br />

coupled (at least with XAML 2006). Compiling the XAML code that includes references to events requires<br />

that the code-behind have a h<strong>and</strong>ler implemented <strong>and</strong> available at compile time. With comm<strong>and</strong>s, the<br />

coupling is loose.<br />

The action that is executed is defined by a comm<strong>and</strong> object. Comm<strong>and</strong>s implement the interface IComm<strong>and</strong>.<br />

Comm<strong>and</strong> classes that are used by WPF are RoutedComm<strong>and</strong> <strong>and</strong> a class that derives from it, RoutedUIComm<strong>and</strong>.<br />

RoutedUIComm<strong>and</strong> defines an additional text for the user interface that is not defined by IComm<strong>and</strong>. IComm<strong>and</strong><br />

defines the methods Execute() <strong>and</strong> CanExecute(), which are executed on a target object.<br />

The comm<strong>and</strong> source is an object that invokes the comm<strong>and</strong>. Comm<strong>and</strong> sources implement the interface<br />

IComm<strong>and</strong>Source. Examples of such comm<strong>and</strong> sources are button classes that derive from ButtonBase,<br />

Hyperlink, <strong>and</strong> InputBinding. KeyBinding <strong>and</strong> MouseBinding are examples of InputBinding derived<br />

classes. Comm<strong>and</strong> sources have a Comm<strong>and</strong> property where a comm<strong>and</strong> object implementing IComm<strong>and</strong><br />

can be assigned. This fires the comm<strong>and</strong> when the control is used, such as with the click of a button.<br />

The comm<strong>and</strong> target is an object that implements a h<strong>and</strong>ler to perform the action. With comm<strong>and</strong> binding,<br />

a mapping is defined to map the h<strong>and</strong>ler to a comm<strong>and</strong>. Comm<strong>and</strong> bindings define what h<strong>and</strong>ler is invoked<br />

on a comm<strong>and</strong>. Comm<strong>and</strong> bindings are defined by the Comm<strong>and</strong>Binding property that is implemented in<br />

the UIElement class. Thus every class that derives from UIElement has the Comm<strong>and</strong>Binding property.<br />

This makes the finding of the mapped h<strong>and</strong>ler a hierarchical process. For example, a button that is defined<br />

within a StackPanel that is inside a ListBox — which itself is inside a Grid — can fire a comm<strong>and</strong>. The<br />

h<strong>and</strong>ler is specified with comm<strong>and</strong> bindings somewhere up the tree — such as with comm<strong>and</strong> bindings of a<br />

Window.<br />

Let’s change the implementation of the BooksDemo project to use comm<strong>and</strong>s instead of the event model.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!