Data binding is a powerful feature of WPF and other XAML frameworks. With binding, you specify a data source and a data target. The binding engine pushes the data from the data source into the data target. The binding engine manages data changes, if a
- [Instructor] Binding is one of my favorite features of WPF and other XAML frameworks. With binding, you specify a data source and a data target. The binding engine pushes the data from the data source into the data target. The target is usually a property on a UI element. The binding engine manages data changes. If a value changes in the source, the target is updated, and when you have two-way binding configured, changes in the target are pushed back into the source. Binding provides additional services, including data conversion and formatting.
Bindings can be defined between UI elements, that means the data source is a property on a UI element, while the data target is a property on another element in the same visual tree. If necessary, you can even have a self-binding between properties on the same element. For this demonstration, I'll look at another type of binding source, I'll use properties on my model class. The UI I'll build is in this TourStopsView.xaml file. The data target in this case will be a list box.
List box is one of the items controls that's available in WPF and items controls are the types of controls that are used to show lists of data. In this case, the list box will show one row for each item in my data source. Let's take a look at the code behind for this file. The code I'll write is in the constructor. There's a couple ways I can set the binding, the way I'm going to set it in this demonstration is I'm going to assign a property on my model class to this ItemSource property on the list box.
I'll press F12 to take a look at the definition of this item source property. This is what I want you to see, the type for this property is IEnumerable, that means that any source that implements IEnumerable can be assigned to the item source property. That means you can work with arrays, list of T, and a very common one you work with in WPF is a special collection class called observable collection of T. For this example I'll just do a list of T. Back to my code. Now what I need to do is find the data source, so let me show you my model class, I'm working with this TourSource class, and this TourStop class.
So the TourStop class contains information about each of our tour stops, like the StopNumber, and the stop Name, location, Description, and so on. And then I have the TourSource, which has this private list of TourStop. In the constructor I populate that private variable, and then down here on line 101 I have a method called GetAllTourStops, so if I call this GetAllTourStops, it will return a list of TourStop, and I can use those results as my data source.
Copy my class name, switch over to my code behind, I'll need to resolve this in the lightbulb here, so I'll click on that, and I'll add this using statement. Now I'll call GetAllTourStops. Now let's review what I've done. This is my data source, this is my data target, this is a property on a UI element. This also happens to be a dependency property, which is important, that's how the databinding engine works.
Now the next step is to add this user control to my main UI. Go over to MainWindow, find the tab, it's this first tab here, again I'll zoom in so you can see what I'm talking about, this Tour Stops tab, so I'll remove this grid. You remember that I created this name space called views in another demo, so I can use that alias, and choose TourStopsView.
I'm not seeing anything in the designer because the code has to run. There's a way to get it to show up in the designer, but you'll have to do your binding completely in XAML. So what I'll need to do is run this and see what it looks like in the running application. There you go, so there's each of the items. Now it doesn't look right yet, and you probably know why this happened, but let me explain, is that I told it to bind to the TourStop class, and the list box doesn't know what to show, or the databinding engine doesn't know what to show, so what it does is it calls to string on the TourStop class, and since I didn't overwrite that method in my TourStop class, all I get returned is the name of the type, so that's what we're seeing here, TailorMadeTours.Models.TourStop.
So the solution is to go and inform the binding engine what to show here, and the best way to do that in WPF is to use what's called a data template, and that's what I'll show in the next video.
- Recognize the pluses of choosing Windows Presentation Foundations over other Microsoft UI frameworks.
- Evaluate the different options available with Windows Presentation Foundation project types.
- Devise a Windows Presentation Foundation project in Visual Studio.
- Break down the assemblies and parts of Windows Presentation Foundations.
- Use XAML editor and editor settings.
- Selecting data binding to present data.
- Compiling interaction code for an application.
- Use control templates, 3D parts, and effects in Windows Presentation Foundations.