Bindable Properties in Xamarin Forms

Binding is the process of connecting a BindableProperty to a property in the BindingContext. The most common scenario for this is a property in UI Element to a property in the ViewModel. Have a look at Binding in Xamarin Forms, if you are unfamiliar with binding. When you are binding to a property in your ViewModel, your ViewModel property is a simple property that raises a OnPropertyChanged notification, each time a new vale is set. A Bindable Property looks at the other side, if you want to Bind a property to it, e.g. the Text property on a Label. The Text property is a Bindable property, meaning it can be bound to.

Bindable Property

Here is an example of a Bindable Property. A bindable property consists of two parts, the first is the BindableProperty declaration.

     public static readonly BindableProperty TextProperty = BindableProperty.Create(
                nameof(Text),
                typeof(string),
                typeof(Label),
                string.Empty);

To break this down,
nameof(Text) : This is supplying the name of the property that stores the information.
typeof(string) : This is the type of the property.
typeof(Label) : This is the type of the class the property is in.
string.Empty  : This is just the default value.

Note: You may have previously seen defining a BindableProperty via generics. This is now obsolete as Xamarin Forms tries to remove Expression Trees from their library to increase speed and remove dependencies.

Moving on to the second part, is a property to store the information.

private string _text;
public string Text
{
    get { return _text; }
    set
    {
        _text = value;
        OnPropertyChanged();
    }
}

The name of the property must be the same name as the Bindable Property, minus the suffix Property. Otherwise it will not work, as Xamarin Forms uses naming conventions to connect the two,. hence TextProperty must have a corresponding property called Text.

Changing Values

If you want to change the value of this property, while you can set the property directly and it will raise an OnPropertyChanged event, this won’t set the value back to any bindings of this property. You may also notice, that if you breakpoint either the get or set in the property, on the first load it will hit, but it never will again.

If you want to perform an action on a BindableProperty change, you need to attach to one of its property change events. PropertyChanging, which is just before it is changed and PropertyChanged, which is run just after it has changed.

public static readonly BindableProperty TextProperty = BindableProperty.Create(
                nameof(Text),
                typeof(string),
                typeof(Label),
                string.Empty,
                propertyChanging: (bindable, oldValue, newValue) =>
                {
                    var control = bindable as Label;
                    var changingFrom = oldValue as string;
                    var changingTo = newValue as string;
                });

From the example, you receive the instance of the control, and the old and new values. You receive the instance of the control, because this is a static property and hence you can’t call any instance method or field from within a static object.

When the property in the ViewModel changes, and a PropertyChanged event is raised, the Binding Engine will use the BindingProperty to set the new value and call any property changed functions.

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer |
Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

2 Comments

  1. Rudy

    Hello. I’m looking for a snippet to create Bindable properties and also Attached properties (easy to find for other Xaml apps). Someone?