嵌套ObservableCollection中的RaisePropertyChanged不更新

时间:2019-03-18 11:04:52

标签: c# wpf mvvm

我现在使用嵌套的ObservableCollection用内部ObservableCollection填充DataGrid的行,该内部ObservableCollection包含有关每个单元格的信息,如下所示:

    public class MemoryTable : INotifyPropertyChanged
    {
        string _Address;
        public string Address
        {
            get
            {
                return _Address;
            }
            set
            {
                if (_Address != value)
                {
                    _Address = value;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
        }

        bool _NextRowOverflow;
        public bool NextRowOverflow
        {
            get
            {
                return _NextRowOverflow;
            }
            set
            {
                if (_NextRowOverflow != value)
                {
                    _NextRowOverflow = value;
                }
            }
        }
        private ObservableCollection<DataAssets> _DataSpace;
        public ObservableCollection<DataAssets> DataSpace
        {
            get
            {
                return _DataSpace;
            }
            set
            {
                _DataSpace = value;
                RaisePropertyChanged("DataSpace");
            }
        }

        public MemoryTable()
        {
            DataSpace = new ObservableCollection<DataAssets>();
        }

        public class DataAssets : INotifyPropertyChanged
        {
            string _Addresses;
            public string Addresses
            {
                get
                {
                    return _Addresses;
                }
                set
                {
                    if (_Addresses != value)
                    {
                        _Addresses = value;
                        RaisePropertyChanged("Addresses");
                    }
                }
            }

            string _Values;
            public string Values
            {
                get
                {
                    return _Values;
                }
                set
                {
                    if (_Values != value)
                    {
                        _Values = value;
                        RaisePropertyChanged("Values");
                    }
                }
            }

            string _ToolTip;
            public string ToolTip
            {
                get
                {
                    return _ToolTip;
                }
                set
                {
                    if (_ToolTip != value)
                    {
                        _ToolTip = value;
                        RaisePropertyChanged("ToolTip");
                    }
                }
            }

            Brush _Color;
            public Brush Color
            {
                get
                {
                    return _Color;
                }
                set
                {
                    if (_Color != value)
                    {
                        _Color = value;
                        RaisePropertyChanged("Color");
                    }
                }
            }

            string _ConvertedValue;
            public string ConvertedValue
            {
                get
                {
                    return _ConvertedValue;
                }
                set
                {
                    if (_ConvertedValue != value)
                    {
                        _ConvertedValue = value;
                        RaisePropertyChanged("ConvertedValue");
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            void RaisePropertyChanged(string prop)
            {
                if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
            }
        }
    }
}

并希望它在我每次更改DataAsset类中的值之一时进行更新,但是每当我通过这种方式更改值时:

NextRow.DataSpace[i].Color = Brushes.Yellow;

所有单元格都没有更新,为此我做出的解决方法是清除并重写整个ObservableCollection,如下所示:

            ObservableCollection<MemoryTable> Temp = new ObservableCollection<MemoryTable>();
        foreach (var item in MemoryTable)
        {
            if (Temp.IndexOf(item) < 0)
            {
                Temp.Add(item);
            }
        }

        MemoryTableDisplay.Clear();
        foreach (var item in Temp)
        {
            if (MemoryTableDisplay.IndexOf(item) < 0)
            {
                MemoryTableDisplay.Add(item);
            }
        }

通过这种方法,我可以强制UI显示更改,但是,当进一步处理更大的数据集时,此方法需要花费很长时间才能完成,是否有可能使内部属性导致更新整个ObservableCollection吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

请尝试这个。

NextRow.DataSpace[i].Color = Brushes.Yellow;

再写下这样的一条语句。

NextRow.DataSpace= NextRow.DataSpace;