BindableBase

  • 4 minutes to read

The class implements the INotifyPropertyChanged interface and includes the API to declare bindable property with minimum coding.

Bindable Properties

To declare bindable properties, do the following:

  • inherit your ViewModel from the class;
  • use the GetProperty and SetProperty methods in property getters and setters.

Property values are stored in an internal dictionary.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get { return GetProperty<string>(); } 
        set { SetProperty(value); }
    }
}

The class exposes the GetProperty and SetProperty overloads that use the CallerMemberNameAttribute. You do not have to pass a property name to these methods as a parameter. This simplifies the bindable property declaration.

The SetProperty method returns True or False values that indicate whether a property has been changed. If you set the same value to the property, the SetProperty method returns False and change notifications are not sent.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get { return GetProperty<string>(); }
        set {
            if (SetProperty(value))
                NotifyFullNameChanged();
        }
    }
}

Run Custom Code When Property Value Is Changed

The SetProperty method has overloads that take a callback method as a parameter. This callback is invoked after a property value has been changed.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get { return GetProperty<string>(); }
        set { SetProperty(value, changedCallback: OnFirstNameChanged); }
    }
    void OnFirstNameChanged() {
        //...
    }
}

Manually Raise INotifyPropertyChanged.PropertyChanged

If you need to raise the INotifyPropertyChanged.PropertyChanged event for a specific property, use the RaisePropertyChanged/RaisePropertiesChanged method.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get { return GetProperty<string>(); }
        set { SetProperty(value, changedCallback: NotifyFullNameChanged); }
    }

    public string LastName {
        get { return GetProperty<string>(); }
        set { SetProperty(value, changedCallback: NotifyFullNameChanged); }
    }

    public string FullName { get { return FirstName + " " + LastName; } }

    void NotifyFullNameChanged() {
        RaisePropertyChanged(nameof(FullName));
    }
}

Using Properties With Backing Fields

An application’s performance can decrease when a property is frequently accessed or updated because property values are stored in the Dictionary. To improve the application’s performance, store property values in backing fields.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    string _FirstName;
    public string FirstName {
        get { return _FirstName; }
        set { SetProperty(ref _FirstName, value, nameof(FirstName), changedCallback: NotifyFullNameChanged); }
    }
}