Chapter 9. Controls, binding, and MVVM
This chapter covers
- Data and element binding
- The Model-View-ViewModel pattern
- Working with controls and lists
- Creating and using value converters
In previous chapters, you’ve learned how to display information on the screen, how to position it, and how to make it pretty. Those are all important skills to master to be an effective XAML developer or designer. What comes next is adding interaction both with the user and with data.
The first approach that most developers use when confronted with a new UI language or markup is to assign property values from code using simple control.property = value assignments. This is certainly familiar territory, but it doesn’t leverage the power of the platform. It also creates too tight a coupling between the UI and the code behind it. The better approach is to use binding, but that has its own learning curve.
When learning binding, you can get into a lot of trouble by going about it in an unstructured way. It’s relatively easy to bind to a property exposed in the code-behind, for example, but later you start running into issues where the exact binding path being used is next to impossible to figure out and debug. The code gets into a twisted knot, and you find yourself having to do things like manually setting contexts and bindings at different levels of the UI.