DataBinding Power Moves You May Not Be Using (Yet)

David Ortinau

DataBinding is a fantastic feature that cleanly decouples your UI from the backing data models while providing the plumbing to move data back and forth as you need. But, are you getting the most out of the Binding features in Xamarin.Forms? Building on our previous Introduction to DataBinding and Advanced Data Binding posts, I’ll show you some of our most powerful features that you should be using.

Compiled Bindings in XAML Compiled is faster. It also means you get data type validation at compile time. The first requirement is to make sure you’ve enabled XAML Compilation. It’s really easy. Add this assembly tag outside your application’s Namespace once.

using Xamarin.Forms.Xaml;
...
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace ContosoAir
{
  ...
}

Then, to benefit from compiled bindings, you need to specify the x:DataType in your XAML, typically at the highest level or the wrapping element of the children that you’re binding. A good rule to keep in mind is to set the X:DataType at the same level where you set the BindingContext. Below is an example page that display a list of deals. It uses the MVVM pattern, so the current BindingContext of this page is a ViewModel with an ObservableCollection<Deal> called MyDeals.

namespace ContosoAir.Clients.Models
{
public class Deal
{
public int Id { get; set; }
public string CityName { get { return ToName; } }
public string CityImage { get { return ToName; } }
public double Price { get; set; }
public string FromName { get; set; }
public string FromCode { get; set; }
public string DepartTime { get; set; }
public string ArrivalTime { get; set; }
public string Hours { get; set; }
public string ToName { get; set; }
public string ToCode { get; set; }
public int Stops { get; set; }
}
}

The model Deal is basic, and doesn’t implement INotifyPropertyChanged. Below you see this collection bound to the ListView and then the x:DataType set on the DataTemplate.

 


...

...








...

XAMLC will generate the code to access the properties, and hook (not in this case) to PropertyChanged.

Note the xmlns reference supports the using instead of clr-namespace when in the same assembly.

Here is the DealControl referenced in the previous code showing that it’s just a simple binding.



...

You may re-specify the DataType at any point in your view hierarchy.

OneWayToSource When you want your binding to be light as a feather, and only need to make sure your source is updated as the user makes changes, then use the OneWayToSource mode.

OneWayToSource as defined in the API docs:

Indicates that the binding should only propagates changes from target (the BindableObject) to source (usually the View Model). This is mainly used for read-only BindableProperty values.

This is useful for when you have input fields that are entry only and you want the values reflected on your ViewModel.


Because the source doesn’t issue property changed events, it’s very efficient. For an additional example, check out this sample project that shows binding one way between controls.

StringFormat You have a lot of inline flexibility to format your content by leveraging StringFormat with bindings. Here are a few quick examples:

Optimize Your Bindings

The latest version of Xamarin.Forms, 2.4.0, delivers the best XAML DataBinding performance to date. Update the NuGets in all your projects today and try out these tips.

Remember to check out our previous articles on DataBinding, and our excellent documentation guides:

Discuss this post on the Xamarin Forums.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Mohamed El-Shawaf 0

    Can compiled binding be applied in X.F v.3.6.0.220655?
    btw, the code snippets are broken.

Feedback usabilla icon