
Adding data bindings
Data binding is the heart and soul of MVVM. This is the way that the Views and the ViewModel communicate with each other. In Xamarin.Forms, we need two things to make data binding happen:
- We need an object to implement INotifyPropertyChanged.
- We need to set the BindingContext of the page to that object. We already do this on both the ItemView and the MainView.
A really useful feature of data binding is that it allows us to use two-way communication. For example, when data binding text to an Entry control, the property on the data-bound object will be updated directly. Consider the following XAML:
<Entry Text="{Binding Title} />
To make this work, we need a property named Title on the object that is a string. We have to look at the documentation, define an object, and let Intellisense provide us with a hint to find out what type our property should be.
Controls that perform some kind of action, like a Button, usually expose a property called Command. This property is of the ICommand type and we can either return a Xamarin.Forms.Command or an implementation of our own. The Command property is explained in the next section, where we will use it to navigate to the ItemView.