Data binding to an object
Up to this point, we have learned how to create CLR properties with the INotifyPropertyChanged
interface; we have also learned about the dependency property with a simple data type. There are many instances when you need to bind an object of some class/model to an UI and display its associated properties necessary.
In this recipe, we will learn how to do object data binding to show and retrieve information to and from the user.
Getting ready
Let's open the Visual Studio instance and create a new project named CH04.ObjectBindingDemo
. Make sure you select the proper WPF application project type.
How to do it...
Perform the following steps to create the model and dependency property, and bind the data to the UI controls, so that, when a change happens in underlying data, it automatically reflects in the UI:
- First, we need to create a data model. From
Solution Explorer
, right-click on the project and navigate to the context menu entryAdd
|Class...
and create a class file...