Data binding to a collection
As we learned about object data binding to show a single object on the UI, let's begin with binding a collection of data objects in a UI to display all the records to the user. We will discuss it in this recipe.
Getting ready
Open a Visual Studio instance and create a new project called CH04.CollectionBindingDemo
. Make sure you use the WPF application project template.
How to do it...
Perform the following steps to create a collection data model and bind it to the UI, using a DataGrid
control:
- Inside the
Solution Explorer
, right-click on the project. From the context menu, navigate toAdd
|Class...
to create a class file namedEmployee.cs
. - Open the
Employee.cs
file and replace the class implementation with the following code:
public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string Department { get; set; } }
- Navigate to the
MainWindow.xaml.cs
file and add the followingusing
statement to define...