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.

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

simple object binding<br />

For binding to CLR objects, with the .<strong>NET</strong> classes you just have to define properties, as shown in the Book<br />

class example <strong>and</strong> the properties Title, Publisher, Isbn, <strong>and</strong> Authors. This class is in the Data folder of<br />

the BooksDemo project.<br />

using System.Collections.Generic;<br />

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

{<br />

public class Book<br />

{<br />

public Book(string title, string publisher, string isbn,<br />

params string[] authors)<br />

{<br />

this.Title = title;<br />

this.Publisher = publisher;<br />

this.Isbn = isbn;<br />

this.authors.AddRange(authors);<br />

}<br />

public Book()<br />

: this("unknown", "unknown", "unknown")<br />

{<br />

}<br />

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

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

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

private readonly List authors = new List();<br />

public string[] Authors<br />

{<br />

get<br />

{<br />

return authors.ToArray();<br />

}<br />

}<br />

}<br />

}<br />

public override string ToString()<br />

{<br />

return Title;<br />

}<br />

code snippet BooksDemo/Data/Book.cs<br />

In the XAML code of the user control BookUC, several labels <strong>and</strong> TextBox controls are defined to display<br />

book information. Using Binding markup extensions, the TextBox controls are bound to the properties<br />

of the Book class. With the Binding markup extension, nothing more than the Path property is defined to<br />

bind it to the property of the Book class. There’s no need to define a source because the source is defined<br />

by assigning the DataContext, as you can see in the code-behind that follows. The mode is defined by its<br />

default with the TextBox element, <strong>and</strong> this is two-way binding:<br />

<br />

<br />

<br />

code snippet BooksDemo//BookUC.xaml<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!