仅查看Model OnPropertyChanged上的更新,但不查看ViewModel OnPropertyChanged中的更新

问题描述 投票:0回答:1

我有一个ViewModel托管Model类的属性,其中ViewModel和Model都实现了INotifyPropertyChanged

但是,如果模型中的属性已更改,则View仅更新。如果在ViewModel中更改了属性,则不会更新视图。

型号基础:

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));
    }
}

模型(添加和删除Positionen-Collection显示在视图中):

public class ChildModel : BaseModel 
{

    private ObservableCollection<SubModel> positionen;

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

ViewModel基础:

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子项(此处属性的更新未显示在视图中):

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")); }
    }
}

如果我更新后面的代码中的文本属性,更新不会显示在视图中。我更新Aktueller Datensatz.Is,变化显示得很好。

我怎样才能解决这个问题。如果需要更多代码,请告诉我。

c# wpf data-binding
1个回答
2
投票

根据以下定义,

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

BaseViewModel不是来自INotifyPropertyChanged所以观点不知道它的变化。

在上面的代码中,INotifyPropertyChanged是对T的约束,其中T必须来自BaseModelINotifyPropertyChanged

更新到

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

因为BaseModel已经来自INotifyPropertyChanged

© www.soinside.com 2019 - 2024. All rights reserved.