Data Binding

Sooner or later (sooner, if you are writing typical commercial applications), you'll need to associate your presentation widgets with persistent data. That data may come from the Internet, or it may come from XML files, email, your operating system, or a database. OK, let's be blunt: most often it will come from a relational database, and if you're programming in the Microsoft world, we'll go so far as to say that it will most often come from SQL Server (though both authors have written commercial applications that used gigabytes of data from databases that were not written in Redmond!).

The ability to bind a control to a data source (of whatever type) is critical in creating efficient professional programs, and WPF would be nothing more than a very impressive toy if it did not support the sophisticated data binding capabilities to which ASP.NET and Windows Forms programmers have grown accustomed.

Data binding in WPF allows you to decouple your user interface from the underlying data (whether it is from SQL Server, an XML file, or some other data source); it thus scales better, performs better, and is more maintainable than more tightly coupled approaches.

Creating a CheckOut Application in Visual Studio

For the next example, you're going to combine XAML and a business class to display a simple shopping cart, as shown in Figure 3-17. You will populate the listbox by binding to a data source you'll declare in the XAML.

Fire up Visual Studio and create a new WPF application ...

Get Programming .NET 3.5 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.