Mister 832
Mister 832

Reputation: 1221

View only updates on Model OnPropertyChanged but not in ViewModel OnPropertyChanged

I have a ViewModel hosting properties of Model classes, where both the ViewModel and the Model implement INotifyPropertyChanged.

However, the View only updates if the properties in the Model are changed. If a property is changed in the ViewModel, the View is not updated.

Model base:

public class BaseModel : INotifyPropertyChanged
{
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; OnPropertyChanged(new PropertyChangedEventArgs("Id")); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyChangedEventArgs.PropertyName));
    }
}

Model (Adding and Removing to the Positionen-Collection are shown in the View):

public class ChildModel : BaseModel 
{

    private ObservableCollection<SubModel> positionen;

    public ObservableCollection<SubModel> Positionen
    {
        get { return positionen; }
        set { positionen = value; OnPropertyChanged(new PropertyChangedEventArgs("Positionen")); }
    }
}

ViewModel base:

public abstract class BaseViewModel<T> where T : BaseModel , INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;


    protected void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
    {
        PropertyChanged?.Invoke(this, propertyChangedEventArgs);
    }


    public abstract ObservableCollection<T> DatensatzListe { get; set; }

    public abstract T AktuellerDatensatz { get; set; }

}

ViewModel child (updates to the properties here are not shown in the View):

public class ChildViewModel : BaseViewModel<ChildModel >
{
    public override ObservableCollection<ChildModel > DatensatzListe
    {
        get { return DatensatzListe; }
        set { DatensatzListe = value; }
    }

    private ChildModel aktuellerDatensatz;

    public override ChildModel AktuellerDatensatz
    {
        get { return aktuellerDatensatz; }
        set { aktuellerDatensatz = value; OnPropertyChanged(new PropertyChangedEventArgs("AktuellerDatensatz")); }
    }

    private string tesxt;

    public string Tesxt
    {
        get { return tesxt; }
        set { tesxt = value; OnPropertyChanged(new PropertyChangedEventArgs("Tesxt")); }
    }
}

If I update the Tesxt Property in the Code behind, updates are not shown in the View. I update AktuellerDatensatz.Id, the change shows up just fine.

How can I fix this. Please let me know, if more code is needed.

Upvotes: 0

Views: 82

Answers (1)

Nkosi
Nkosi

Reputation: 247571

According to the following definition,

public abstract class BaseViewModel<T> where T : BaseModel , INotifyPropertyChanged

BaseViewModel is not derived from INotifyPropertyChanged so the view is unaware of its changes.

In the above code the INotifyPropertyChanged is a constraint on T where T must be derived from BaseModel and INotifyPropertyChanged

Update to

public abstract class BaseViewModel<T>: INotifyPropertyChanged 
    where T : BaseModel

as BaseModel is already derived from INotifyPropertyChanged

Upvotes: 2

Related Questions