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.

1036 ❘ ChaPTer 36 business ApplicAtiOns with wpf<br />

Binding supports several binding modes between the target <strong>and</strong> source. Binding can be one-way, where the<br />

source information goes to the target, but if the user changes information in the user interface, the source<br />

does not get updated. For updates to the source, two-way binding is required.<br />

The following table shows the binding modes <strong>and</strong> their requirements.<br />

binding mode<br />

One-time<br />

One-way<br />

Two-way<br />

One-way-to-source<br />

desCriPTion<br />

Binding goes from the source to the target <strong>and</strong> occurs only once when the application<br />

is started or the data context changes. Here, you get a snapshot of the data.<br />

Binding goes from the source to the target. This is useful for read-only data, because<br />

it is not possible to change the data from the user interface. To get updates to the user<br />

interface, the source must implement the interface INotifyPropertyChanged.<br />

With two-way binding, the user can make changes to the data from the UI. Binding<br />

occurs in both directions — from the source to the target <strong>and</strong> from the target to the<br />

source. The source needs to implement read/write properties so that changes can be<br />

updated from the UI to the source.<br />

With one-way-to-source binding, if the target property changes, the source object gets<br />

updated.<br />

WPF data binding involves many facets beside the binding modes. This section gives you details on binding<br />

to XAML elements, binding to simple .<strong>NET</strong> objects, <strong>and</strong> binding to lists. Using change notifications, the<br />

UI is updated with changes in the bound objects. You will read about getting the data from object data<br />

providers <strong>and</strong> directly from the code. Multibinding <strong>and</strong> priority binding demonstrates different binding<br />

possibilities other than the default binding. You will also read about dynamically selecting data templates,<br />

<strong>and</strong> validation of binding values.<br />

Let’s start with the BooksDemo sample application.<br />

booksdemo application<br />

In this section, you create a new WPF application named BooksDemo to use throughout this chapter with<br />

data binding <strong>and</strong> comm<strong>and</strong>ing.<br />

Change the XAML file MainWindow.xaml <strong>and</strong> add a DockPanel, a ListBox, a Hyperlink, <strong>and</strong> a<br />

TabControl:<br />

<br />

<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!