Skip to main content

BindableBase Class

A ViewModel that supports the INotifyPropertyChanged interface and allows you to use GetProperty and SetProperty methods to implement bindable properties.

Namespace: DevExpress.Mvvm

Assembly: DevExpress.WinUI.Mvvm.v23.2.dll

NuGet Package: DevExpress.WinUI

Declaration

[DataContract]
public abstract class BindableBase :
    INotifyPropertyChanged

Remarks

Bindable Properties

The BindableBase class contains the protected GetValue and SetValue overloads that use the CallerMemberNameAttribute. You do not have to pass a property name to these methods as a parameter.

Run Demo: BindableBase Module in the WinUI MVVM Demo

To declare bindable properties, follow the steps below:

  1. Inherit your ViewModel from the BindableBase class.
  2. Define a property.
  3. Use the GetValue and SetValue methods in the property’s get/set methods.
using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName { 
        get => GetValue<string>(); 
        set => SetValue(value); 
    }
}

When you assign a value to the property, the BindableBase class detects whether the property value is changed.

If the specified property’s value is changed, the SetValue method returns true. If the property’s new value is equal to the previous value, the method returns false.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get => GetValue<string>();
        set {
            if (SetValue(value))
                NotifyFullNameChanged();
        }
    }
}

Run Custom Code When Property Value Is Changed

The SetValue method contains overloads that take a callback method as a parameter. When the property value is changed, the BindableBase class invokes this callback method.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get => GetValue<string>();
        set => SetValue(value, changedCallback: OnFirstNameChanged);
    }
    void OnFirstNameChanged() {
        //...
    }
}

Raise the INotifyPropertyChanged.PropertyChanged Event

Call the RaisePropertyChanged/RaisePropertiesChanged methods to raise the PropertyChanged event for a specified property.

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    public string FirstName {
        get => GetValue<string>();
        set => SetValue(value, changedCallback: NotifyFullNameChanged);
    }

    public string LastName {
        get => GetValue<string>();
        set => SetValue(value, changedCallback: NotifyFullNameChanged);
    }

    public string FullName => $"{FirstName} {LastName}";

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

Use Properties With Backing Fields

An application stores its property values in Dictionaries. When you frequently access/update an application’s property values, the application performance may decrease. To bypass this performance issue, you can store property values in backing fields. The BindableBase class allows you to get and set values of properties that are stored in backing fields.

The following code sample stores values of FirstName and LastName properties in backing fields and calls the RaisePropertyChanged method when at least one of these property values is changed:

using DevExpress.Mvvm;

public class ViewModel : BindableBase {
    string _FirstName;
    public string FirstName {
        get => _FirstName;
        set => SetValue(ref _FirstName, value, changedCallback: NotifyFullNameChanged);
    }

    string _LastName;
    public string LastName {
        get => _LastName;
        set => SetValue(ref _LastName, value, changedCallback: NotifyFullNameChanged);
    }
    public string FullName => $"{FirstName} {LastName}";

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