Skip to content

NotifyObject

Patrick Schimmel edited this page May 24, 2021 · 2 revisions

NotifyObject

Namespace: ES.Tools.MVVM (ES.Tools.Core)

The NotifyObject is a base class for all classes that need to raise PropertyChanged events. It implements interface INotifyPropertyChanged.

This is mainly used on the ViewModel level when using WPF data binding to notify the View when properties in the ViewModel have changed.

Usage

The example consists of an Employee class which inherits from NotifyObject. Whenever the properties FirstName or LastName is changed, a PropertyChanged. event is triggered by calling OnPropertyChanged.

Also a PropertyChanged event it triggeredd for the property FullName.

Calling OnPropertyChanged without arguments automatically uses the name of the surrounding property.

public class Employee : NotifyObject
{
  private string _firstName;
  private string _lastName;

  public string FirstName
  {
    get => _firstName;
    set
    {
      if (_firstName != value)
      {
        _firstName = value;
        OnPropertyChanged();
        OnPropertyChanged(nameof(FullName));
      }
    }
  }

  public string LastName
  {
    get => _lastName;
    set
    {
      if (_lastName != value)
      {
        _lastName = value;
        OnPropertyChanged();
        OnPropertyChanged(nameof(FullName));
      }
    }
  }

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

Remarks

See also ViewModel.

Clone this wiki locally