Search code examples
c#.netrealmrealm-database

How can I override a property in a RealmObject using realm database?


I want to override the following property using Realm so I can trigger a RaisePropertyChanged for another property:

public int Reps { get; set; }

This doesn't work, because it does not get picked up by Realm as a column in the database:

private int _reps;
public int Reps
{
    get => _reps;
    set
    {
        RaisePropertyChanged("Reps");
        RaisePropertyChanged("RepsInfo");
        _reps = value;
    }
}
public int Reps { get; set; }

For what it's worth RepsInfo looks like this:

public string SetRepInfo { get => $"{Sets}x{Reps}"; }

The reason I want to do a RaisePropertyChanged("RepsInfo") is because all places where I'm using this object does not get an updated SetRepInfo when the Reps is updated.


Solution

  • You should instead override the OnPropertyChanged method. Adapting the example from the docs you need something like:

    protected override void OnPropertyChanged(string propertyName)
    {
        if (propertyName == nameof(Reps))
        {
            RaisePropertyChanged(nameof(SetRepInfo));
        }
    }