Skip to content

πŸ”— Observable Objects

Marc Rousavy edited this page Jul 3, 2018 · 3 revisions

Every object with properties that need to be observed for changes needs to implement the ObservableObject class:

public class User : ObservableObject
{
    ...
}

For view models see: View Models or Observable Objects πŸ“–.

Using this base class' Set function allows for quick notifying properties:

private string _username;
public string Username
{
    get => _username;
    set => Set(ref _username, value);
}

If you are using ReSharper you can define a notify-property-changed-property template.

You can also force a PropertyChanged invocation by using the Notify function:

public string UserString => $"{Title} {Username}";

public string Username
{
    get => _username;
    set
    {
        Set(ref _username, value);
        Notify(nameof(UserString));
    }
}
Clone this wiki locally