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.

Data Binding ❘ 1055<br />

}<br />

}<br />

}<br />

return null;<br />

code snippet BooksDemo/Utilities/BookTemplateSelector.cs<br />

For accessing the class BookDataTemplateSelector from XAML code, the class is defined within the<br />

Window resources:<br />

<br />

code snippet BooksDemo/BooksUC.xaml<br />

Now the selector class can be assigned to the ItemTemplateSelector property of the ListBox:<br />

<br />

When running the application, you can see different data templates based on the publisher, as shown in<br />

Figure 36-9.<br />

figure 36-9<br />

binding to Xml<br />

WPF data binding has special support for binding to XML data. You can use XmlDataProvider as a<br />

data source <strong>and</strong> bind the elements by using XPath expressions. For a hierarchical display, you can use the<br />

TreeView control <strong>and</strong> create the view for the items by using the HierarchicalDataTemplate.<br />

The following XML file containing Book elements is used as a source in the next examples:<br />

<br />

<br />

<br />

Professional <strong>C#</strong> 2008<br />

Wrox Press<br />

Christian Nagel<br />

Bill Evjen<br />

Jay Glynn<br />

Karli Watson<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!