Skip to main content
A newer version of this page is available. .

BindableBase

  • 4 minutes to read

The BindableBase class implements the INotifyPropertyChanged interface and provides capabilities for easy implementation of bindable properties with the GetProperty and SetProperty methods.

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

The first parameter of the GetProperty and SetProperty methods is a lamda-expression that returns the property used for identifying the target property name (property names are internally obtained with the BindableBase.GetPropertyName<T> static method).

Property values are stored in an internal Dictionary (the GetProperty method uses this dictionary to get a property value based on the property name, the SetProperty method stores a property value using the property name as a key). This approach simplifies code, and allows code checking during compilation and renaming properties with standard Visual Studio refactoring capabilities.

The SetProperty method returns True or False values that indicate whether or not a property has been successfully changed (if you set the same value to the property, the SetProperty method returns False and changing notifications are not sent). Some SetProperty method overloads also take a callback method as a parameter. This callback is invoked after the field has been changed.

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

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

public class ViewModel : BindableBase {
        public string FullName {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }
        public string FirstName {
            get { return GetProperty(() => FirstName); }
            set { SetProperty(() => FirstName, value, OnFirstNameChanged); }
        }
        public string LastName {
            get { return GetProperty(() => LastName); }
            set {
                if(SetProperty(() => LastName, value))
                    RaisePropertyChanged(() => FullName);
            }
        }
        void OnFirstNameChanged() {
            RaisePropertyChanged(() => FullName);
        }
    }

In rare cases, application performance can deteriorate when a property is frequently updated (due to the calculation of property names from lambda-expressions and the accessing of properties from the Dictionary storage). To accommodate these scenarios, use storage variables for properties and calculate property names once from the static constructor using the BindableBase.GetPropertyName<T> method.

public class ViewModel : BindableBase {
        static string Property1Name;
        static ViewModel() {
            Property1Name = BindableBase.GetPropertyName(() => new ViewModel().Property1);
        }

        string property1;
        public string Property1 {
            get { return property1; }
            set { SetProperty(ref property1, value, Property1Name); }
        }
    }