WPF Data Binding in Depth

Data binding is one of the most powerful and important capabilities in WPF applications for building data-driven, loosely coupled, maintainable applications. In this course you will learn how to use the end-to-end capabilities of data binding in WPF including both the most common features as well as more advanced capabilities.
Course info
Rating
(583)
Level
Intermediate
Updated
Jan 9, 2014
Duration
6h 19m
Table of contents
WPF Data Binding in Depth Course Overview
Data Binding Overview
Data Sources
Binding Core Concepts
Unleashing the Full Power of Bindings
Deep Dive into DataTemplates
Design Time Data Binding
Containers and Collection Controls
Data Input Validation
Description
Course info
Rating
(583)
Level
Intermediate
Updated
Jan 9, 2014
Duration
6h 19m
Description

The data binding capabilities of WPF are one of the most powerful parts of the XAML platform. Data binding allows you to keep your XAML UI definition loosely coupled from the logic and data access that gets data into the application that will be used for display purposes. You can use data binding to build rich, interactive, beautifully designed user interfaces that are driven by data of many types. In this course you will learn how to leverage all the features of data binding and will learn enough to feel comfortable tackling everything from simple to complex data binding scenarios. You'll learn about what kinds of data sources you can work with, all the features that the Bindings themselves support, and how to use DataTemplates to provide a custom, reusable XAML rendering of your data objects. You'll learn how to use design time features in Visual Studio to generate data bound UIs or hook up existing ones, and you'll learn how to use the validation features to validate input data and present errors to the user. Along the way, you'll see some demos that use the Model-View-ViewModel (MVVM) pattern and others that do not so you can see the power of data binding regardless of the UI patterns that you follow.

About the author
About the author

Brian Noyes is CTO and Architect at Solliance, an expert technology solutions development company. Brian is a Microsoft Regional Director and MVP, and specializes in rich client technologies including XAML and HTML 5, as well as building the services that back them with WCF and ASP.NET Web API.

More from the author
WPF Productivity Playbook
Intermediate
2h 57m
22 Aug 2016
More courses by Brian Noyes
Section Introduction Transcripts
Section Introduction Transcripts

Data Binding Overview
Hi there. This is Brian Noyes, and in this first module we're going to go through a high-level overview of what data binding is all about in WPF, why you want to use it, and what some of the fundamental concepts are that you'll need to master to understand the rest of the course, where we'll dive deep enough to turn you into a data binding ninja. First I'm going to go through a high-level overview of what data binding is all about and why you want to use it, what some of the benefits are, and what it does for you. Next I'll talk about some of the key concepts, things like bindings and DataContext, and change notifications, and the role that they play in using data binding. Then I'll go a little bit deeper on DataContext and talk about the scoping mechanisms of DataContext and how you can use it to control what data flows to what part of your views. And then we'll finish off the module with a series of demos that starts off with a data-driven form, not using data binding at all, then transforming that to use data binding to see some of the benefits there, and finally, cleaning that up a little bit by consolidating your data sources into a single DataContext object.

Data Sources
Hi, this is Brian Noyes, and in this module we're going to focus on data sources for data binding, where the data comes from to support your UI. Specifically, we're going to talk about the different kinds of data source objects that you can data bind to and some of the support those data objects may need to fully participate in data binding. First we're going to talk about Entity Data Sources, and this includes individual objects that you want to data bind to and their properties for something like a data entry form, as well as collections of entities for something like a DataGrid or a ComboBox. Next we're going to talk about Change Notifications. This is support that these entities will need in order to fully participate in data binding, meaning that if their properties can change behind the scenes, they've got to raise change notifications so that the binding can update the UI and keep it fresh with the real state of the data behind the scenes. Next we're going to talk about some of the other aspects of the support that entities can have for data binding, inducing the ability to back out changes when multiple properties have been changed. Those are called EditableObjects, and CollectionViews, which are things that wrap collections when you data bind to them to maintain the notion of currency or what is the current object within the collection. Towards the end of the module, we'll start talking about other kinds of things besides entities including DataSets and XML Data Sources, and how you can go about data binding to those.

Binding Core Concepts
Hi, this is Brian Noyes. In this module, we're going to dive a little deeper into bindings and get into some of the core concepts of how you can have explicit control over exactly what a binding is doing with respect to its source objects and its paths and the direction of flow of data through that binding. First we're going to dive into binding sources. We've already seen in previous modules how bindings use the DataContext as their source by default. In this section we'll talk about the other options that you have there, including the RelativeSource bindings, ElementName bindings, and Source bindings where you can point to a explicit Source object through those properties on the binding. Next we're going to get into Property Paths in a little more detail. We've seen simple paths that are just a property name in previous demos, and we've actually seen a couple of other examples that dot down. Here we'll just talk a little bit more about exactly what is supported, the fact that you can walk an entire object graph through the property path and even index into arrays. And also we'll talk a little bit about debugging bindings when they're not working and see how you can spot the errors for your bindings in the output window. Finally, we'll close out this module with the mode and direction of bindings. Bindings flow from source to target, by default, and we've seen that you can have two-way bindings that flow from target to source as well. The binding mode property is the thing that lets you control this, and we'll also look into how the default direction is set for a given target property through its DependencyProperty metadata.

Deep Dive into DataTemplates
Hi, this is Brian Noyes, and in this module we're going to dive into Data Templates. Data Templates are a very powerful mechanism of WPF data binding that allows you to provide the visual structure for some data-bound object and have that mapped up at runtime as the objects get rendered out. So we'll look at how DataTemplates work and what the variations on DataTemplates are and how to use them. First we'll talk about the broad concepts of DataTemplates and how they work in general, and how they get mapped out to the data objects that are being rendered out to provide the visual structure for rendering on the screen. Next we'll talk about ExplicitDataTemplates, which are the most direct and simple way to use DataTemplates. As the name implies, it involves a direct hookup between the data-bound control and the template that should be used for a given collection or data object. Next we'll talk about ImplicitDataTemplates, which are a little more magical in the way that they work, but it involves an automatic selection of the appropriate DataTemplate based on the type of the data object that's going to be rendered, and this gives us an awful lot of power for handling heterogeneous collections of derive types of some data object type, and it becomes vitally important in Model-View-ViewModel scenarios, which we'll use in the demos to render out different Views for different ViewModel types. Next, we'll talk about DataTemplateSelectors, which give you a little more explicit control, but still dynamic control to switch which DataTemplate you use based on whatever logic you need to invoke to make that selection. And finally, we'll finish off this module with HierarchicalDataTemplates, which are just a specialized form of DataTemplate that work in hierarchical data model scenarios such as presenting data in TreeView control.

Design Time Data Binding
Hi, this is Brian Noyes. In this module, we're going to look into some of the Design Time Data features that Visual Studio has to help you get your UI's hooked up, data-bound, and even generate UI elements based on your data model. We'll also look at how you can use sample data in the Design-Time environment to help get your UI laid out and to have that data to visualize what it's going to look like in the UI as you're designing it. First, we're going to look at two windows within the Visual Studio environment, the Data Sources Window and the Properties Window. Data Sources Window lets you generate data-bound UI elements based on your data model or hook up existing elements, and the Properties Window lets you hook up bindings to individual properties for the elements you're working with in the designer. Next, we're going to look at working with DataTemplates in the designer. Visual Studio carries over some features from Blend where you can actually design DataTemplates in the designer as a stand-alone resource, and then switch back into the main design mode of the designer when you're done. Finally, we'll look at how you can use the overall feature that's referred to as Design Time Data, which is mainly a set of properties and markup extensions that you can use within your XAML that makes certain things available to the designer, but they go away at runtime. And this includes being able to pull in some sample data that will populate your UI in the designer, but not be there at runtime.

Containers and Collection Controls
Hi, this is Brian Noyes. In this module, we're going to dive into some of the containment scenarios and the dynamic generation of contained controls based on data binding. We'll also look in more depth at the common collection controls, including ListBox, ComboBox, TabControl and DataGrid. And we'll finish off by talking a little bit more about collection views, which can be used to modify the collections you're data-bound to. So first up, we're going to dive into what's really going on at runtime when you have a data-driven control and the composition that happens with the UI elements that are generated based on the data. I'll quickly explain what's going on in concept with bullets, and then we'll dive into a demo that hopefully makes it all clear, since it is dynamic in nature. Next, we'll review ComboBoxes and ListBoxes and the key data binding properties and behaviors there. We've seen a lot of this in previous demos, but in those previous demos I just kind of glanced over it and didn't focus one at a time on the properties you'll care about. Next, we'll get into TabControls, which TabControls are more of a structural element, but they can be data-driven themselves. So we'll see a common scenario where you can use TabControls for child view containment that can be dynamically-driven through data binding. Next, we'll get into the Ubiquitous DataGrid and we'll focus on some of the key properties that you'll need to know to really drive a DataGrid in normal data binding scenarios. There are, naturally, all kinds of deep, dark corners in the DataGrid and lots of fancy customizations of appearance and things that are out of scope for this course. What I'm going to focus on are the things that really drive the behavior of the DataGrid, based on it being data-bound. Finally, we'll finish out this module by revisiting Collection Views. I talked about the ICollectionView interface in an earlier module, and we looked at the notion of currency driven by Collection Views. Here we're going to focus on some other things it can do for you in terms of manipulating the collection that you're working with to do things like sorting, filtering, grouping, and master-details presentations.

Data Input Validation
Hi, this is Brian Noyes. In this last module, we're going to look into the validation mechanisms of data bindings and how you can use it to provide validation indications to a user when they put in bad data. First we're going to talk about the simplest mechanism to use, which is to throw an exception when you get bad data and to turn on a flag on the binding to have it give validation indications based on that. After that, we'll look at a built-in mechanism in WPF called ValidationRules. ValidationRules are objects that you implement that encapsulate a rule and get evaluated by a binding when you hook it up to it. If that rule returns an error, then you'll get an error indication on the screen. Next, we'll look at an interface that's been around since. NET 1. 0 called IDataErrorInfo, which is a standard mechanism for indicating validation errors in combination with data binding. When you use IDataErrorInfo, the object that you're binding to implements this interface and it will be queried for errors after the properties are set. Next, we'll look at INotifyDataErrorInfo, which was added in. NET 4. 5 and supports asynchronous validation. It's very similar to IDataErrorInfo, which is a synchronous API, but it supports the notion that you may need to make an asynchronous call to something like a service to determine whether an input value is valid or not, and then once you get the results back, then you want to provide the invalid indication. Next, we'll talk briefly about some binding events that are available that can be handy in dealing with validation and other scenarios as well. We'll talk about how you can customize the validation indications that the built-in controls use for presenting validation errors and how you can provide things like ToolTips with the error strings in them. And then we'll finish off this module by talking about DataAnnotations, which are a standard mechanism in. NET for indicating the validation rules associated with the property through attributes.