Data Binding in MAUI
WPF data binding was extremely powerful and good to work with. It was robust and felt like you could do whatever you wanted with it. .NET MAUI’s data binding is pretty much the same.
What is data binding? Data binding is when we take the value of a property in our C# class and make it the value that drives a property in our MAUI controls.
If you fire up a new MAUI application, you will see something like the image below. In this blog post, we will update the “Clicked 5 times” text to use data binding.
If we look at the original XAML, the code for the button is pretty basic.
In the original version, the click handler updates the button text directly.
Now we are going to change this to use data binding instead. First, we will set the binding context to the current UI control.
Create a new Count
property to handle the value. We will call NotifyPropertyChanged
when we update this property, telling the UI that the property has been updated.
We will create a separate property for the actual button text.
Now we will update the click handler to update the Count
property.
Last, we must update the button control. We must bind the button’s text to the newly created ButtonText
property.
Data binding with MAUI is like the data binding we had with WPF; it feels very familiar. Give it a try; you might like it.