Chapter 10. Using Silverlight Data Binding

In most of the previous examples in this book, we “manually” set properties of XAML elements to display data. In real world applications, things do not always work that easily. Quite often you have a business logic that pulls data from somewhere, processes it, and then exposes the data in the form of an object. To display this object, you may need code again. Well, maybe, maybe not. With data binding, Silverlight provides a useful feature that once again separates code and logic from the presentation layer. Using declarative syntax, you can define which information from a business object goes where. Silverlight does the rest: it retrieves the data, displays it, and can even write the data back to the object, if you so desire.

Data Binding with Markup

To use Silverlight data binding, you first need a binding source—that’s where the data is coming from. Silverlight supports several kinds of sources, including XML files, the results of LINQ queries, and objects. Throughout most of this chapter we will use the last option—objects as sources—to keep the examples simple and self-contained (and to avoid having to add a new external dependency, e.g., a database). However, in the real world, the possibilities are virtually endless.

The data used in these examples will be data on a person or on various persons. Each person is represented by an instance of a class called Person. This class will be defined in every sample application’s Page.xaml.cs code-behind ...

Get Essential Silverlight 2 Up-to-Date now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.