简体   繁体   English

ObservableCollection <> CollectionChanged未触发

[英]ObservableCollection<> CollectionChanged Not Firing

I have a datagrid in a WPF app that is bound to an ObservableCollection like so 我在WPF应用程序中有一个数据网格,该数据网格像这样绑定到ObservableCollection

<DataGrid ItemsSource="{Binding Spring.SpringData, Mode=OneWay}" />

My data displays fine, and I can edit the data in my grid, but it does not fire the PublishSpringChange event when I manually edit the data in the grid. 我的数据显示正常,我可以编辑网格中的数据,但是当我手动编辑网格中的数据时,它不会触发PublishSpringChange事件。 The underlying data changes, but the event does not fire, what am I missing? 基础数据发生更改,但事件未触发,我还缺少什么?

With a model of Spring that has the following 使用具有以下内容的Spring模型

public class Spring : INotifyPropertyChanged
{

    private ObservableCollection<SpringData> _SpringData;

    public ObservableCollection<SpringData> SpringData
    {
        get { return _SpringData; }
    }

     public Spring()
    {
        ....

        _SpringData = new ObservableCollection<SpringData>();
        SpringData.CollectionChanged += PublishSpringChange;

       ...
    }
    private void PublishSpringChange(object sender, NotifyCollectionChangedEventArgs e)
    {
        // Code that does not run!
    }
}

with a SpringData class of 带有一个SpringData类

public class SpringData: BindableBase
{
    private double _force;
    private double _displacement;

    public SpringData(double displacement, double force)
    {
        Displacement = displacement;
        Force = force;
    }

    public double Displacement
    {
        get { return _displacement; }
        set { SetProperty(ref _displacement, value); }
    }

    public double Force
    {
        get { return _force; }
        set { SetProperty(ref _force, value); }
    }
}

INotifyCollectionChanged only fires when you actually modify the collection. 仅当您实际修改集合时才触发INotifyCollectionChanged This is when you Add, Remove, Move, Replace or Reset items in the collection. 这是您在集合中添加,删除,移动,替换或重置项目的时间。 It will not fire when one of the properties in a SpringData object is changed. 更改SpringData对象中的属性之一时,它将不会触发。

In order to listen to changes for a SpringData object, assuming it implements INotifyPropertyChanged , you will need to hook up listeners to the PropertyChanged event of each of the items. 为了侦听SpringData对象的更改,假定该对象实现了INotifyPropertyChanged ,则需要将侦听器连接到每个项目的PropertyChanged事件。

Its quite useful to have a single handler for all properties changing sometimes. 有时为所有属性设置单个处理程序非常有用。 Here's how you can do it. 这是您的操作方法。

Handle CollectionChanged as you are above: 如上所述,处理CollectionChanged:

_SpringData = new ObservableCollection<SpringData>();
SpringData.CollectionChanged += PublishSpringChange;

Now for all added and removed objects to the collection add a handler to PropertyChanged: 现在,对于所有添加和删除到集合中的对象,向PropertyChanged添加一个处理程序:

private void PublishSpringChange(object sender, NotifyCollectionChangedEventArgs e)
{
    foreach (INotifyPropertyChanged added in e.NewItems)
    {
        added.PropertyChanged += SpringDataOnPropertyChanged;
    }

    foreach (INotifyPropertyChanged removed in e.OldItems)
    {
        removed.PropertyChanged -= SpringDataOnPropertyChanged;
    }
}

private SpringDataOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
    //your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM