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.

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

master details binding<br />

Instead of just showing all the elements inside<br />

a list, you might want or need to show detail<br />

information about the selected item. It doesn’t<br />

require a lot of work to do this. The Label <strong>and</strong><br />

TextBox controls are already defined; currently<br />

they only show the first element in the list.<br />

There’s one important change you have to make<br />

to the ListBox. By default, the labels are bound<br />

to just the first element of the list. By setting the<br />

ListBox property IsSynchronizedWithCurren<br />

tItem=“True”, the selection of the list box is set<br />

to the current item. In Figure 36-7 you can see<br />

the result; the selected item is shown in the detail<br />

section labels:<br />

multibinding<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet BooksDemo/BooksUC.xaml<br />

Binding is one of the classes that can be used for data binding. BindingBase is the abstract base class of<br />

all bindings <strong>and</strong> has different concrete implementations. Besides Binding, there’s also MultiBinding <strong>and</strong><br />

PriorityBinding. MultiBinding allows you to bind one WPF element to multiple sources. For example,<br />

with a Person class that has LastName <strong>and</strong> FirstName properties, it is interesting to bind<br />

both properties to a single WPF element:<br />

public class Person<br />

{<br />

public string FirstName { get; set; }<br />

public string LastName { get; set; }<br />

}<br />

figure 36-7<br />

code snippet MultiBindingDemo/Person.cs<br />

For MultiBinding, a markup extension is not available — thus the binding must be specified with XAML<br />

element syntax. The child elements of MultiBinding are Binding elements that specify the binding to the<br />

various properties. Here the FirstName <strong>and</strong> LastName properties are used. The data context is set with the<br />

Grid element to reference the person1 resource.<br />

To connect the properties together, MultiBinding uses a Converter to convert multiple values to one. This<br />

converter uses a parameter that allows for different conversions based on the parameter:<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!