15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet BooksDemo/BooksUC.xaml<br />

For selecting the template, the class BookDataTemplateSelector overrides the method SelectTemplate<br />

from the base class DataTemplateSelector. The implementation selects the template based on the<br />

Publisher property from the Book class:<br />

using System.Windows;<br />

using System.Windows.Controls;<br />

using Wrox.ProCSharp.WPF.Data;<br />

namespace Wrox.ProCSharp.WPF.Utilities<br />

{<br />

public class BookTemplateSelector : DataTemplateSelector<br />

{<br />

public override DataTemplate SelectTemplate(object item,<br />

DependencyObject container)<br />

{<br />

if (item != null && item is Book)<br />

{<br />

var book = item as Book;<br />

switch (book.Publisher)<br />

{<br />

case "Wrox Press":<br />

return<br />

(container as FrameworkElement).FindResource(<br />

"wroxTemplate") as DataTemplate;<br />

case "For Dummies":<br />

return<br />

(container as FrameworkElement).FindResource(<br />

"dummiesTemplate") as DataTemplate;<br />

default:<br />

return<br />

(container as FrameworkElement).FindResource(<br />

"bookTemplate") as DataTemplate;<br />

}<br />

}<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!