INotifyPropertyChanged没有报告我的派生类内部的变化

时间:2015-07-27 14:43:13

标签: c# inotifypropertychanged

我有这个基类:

public class MyFileInfo : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _file;
    private int _bytesSent;

    public MyFileInfo(string file)
    {

    }

    public string File
    {
        get { return _file; }
        set { _file = value; }
    }

    public int BytesSent
    {
        get { return _bytesSent; }
        set
        {
            _bytesSent = value;
            OnPropertyChanged("BytesSent");
        }
    }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new     PropertyChangedEventArgs(propertyName));
        }
}

派生类:

public class MyFile : MyFileInfo
{
    public MyFile(MyFileInfo myFileInfo)
    }
          this.File = pcapInfo.myFileInfo;
          this.BytesSent = pcapInfo.BytesSent;
    }

    public DoWork()
    {
        // here BytesSent is changing
    }
{

好的,我有基础和派生类。 在derive类中,我的属性BytesSent正在改变,但我的UI没有。 这是我的收藏:

private ObservableCollection<MyFile> files{ get; set; }

也许我需要在derive类中定义OnPropertyChanged方法?

1 个答案:

答案 0 :(得分:0)

当项目内的属性发生变化时,ObservableCollection不会通知。它只会在自己更改的列表时通知您,例如是否添加或删除了某个项目。

点击此处了解更多信息:ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)