【问题标题】:ObservableCollection and CollectionChanged EventObservableCollection 和 CollectionChanged 事件
【发布时间】:2010-06-15 02:32:01
【问题描述】:

为什么在以下代码中 collectionchanged 事件没有触发,但我可以看到我添加到 ObservableCollection 的 InventoryBTO 的新实例?

 private ObservableCollection<InventoryBTO> _inventoryRecords;
    public ObservableCollection<InventoryBTO> InventoryRecords
    {
        get { return _inventoryRecords; }
        set { _inventoryRecords = value; }
    }

    private InventoryBTO _selectedRecord;
    public InventoryBTO SelectedRecord
    {
        get { return _selectedRecord; }
        set 
        {
            if (_selectedRecord != value)
            {
                _selectedRecord = value;
                OnPropertyChanged(new PropertyChangedEventArgs("SelectedRecord"));
            }
        }
    }

    public InventoryViewModel()
    {
        if (_inventoryRecords == null)
        {
            InventoryRecords = new ObservableCollection<InventoryBTO>();
            this.InventoryRecords.CollectionChanged += new NotifyCollectionChangedEventHandler(InventoryRecords_CollectionChanged);
        }

        _inventoryRecords = InventoryListBTO.GetAllInventoryRecords();
    }

    void InventoryRecords_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {

    } 

【问题讨论】:

  • 看起来你连接到你的ctor中的集合,然后立即用一个完全不同的集合覆盖引用。也许你打算做一个 AddRange 而不是覆盖?无论哪种方式,您创建然后连接到的可观察集合早已不复存在

标签: c# observablecollection


【解决方案1】:

问题是您将私有成员分配给您从方法中返回的ObservableCollection 的新实例。因此,正在发生的事情是,您连接到一个集合的事件,但随后吹走该实例并用您从未连接过事件处理程序的新实例替换它。这是你可以做的。创建一个继承自ObservableCollection的类并添加addrange方法:

public class RangeObservableCollection<T> : ObservableCollection<T>
{
    private bool supressEvents = false;

    public void AddRange(IEnumerable<T> items)
    {
        supressEvents = true;
        foreach (var item in items)
        {
            base.Add(item);
        }
        this.supressEvents = false;
        this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, items.ToList()));

    }

    protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
        if (!this.surpressEvents)
        {
            base.OnCollectionChanged(e);
        }
    }
}

然后,您可以将您的班级更改为:

private RangeObservableCollection<InventoryBTO> _inventoryRecords;
public RangeObservableCollection<InventoryBTO> InventoryRecords
{
    get { return _inventoryRecords; }
    set { _inventoryRecords = value; }
}

private InventoryBTO _selectedRecord;
public InventoryBTO SelectedRecord
{
    get { return _selectedRecord; }
    set 
    {
        if (_selectedRecord != value)
        {
            _selectedRecord = value;
            OnPropertyChanged(new PropertyChangedEventArgs("SelectedRecord"));
        }
    }
}

public InventoryViewModel()
{
    if (_inventoryRecords == null)
    {
        InventoryRecords = new ObservableCollection<InventoryBTO>();
        this.InventoryRecords.CollectionChanged += new NotifyCollectionChangedEventHandler(InventoryRecords_CollectionChanged);
    }

    this.InventoryRecords.AddRange(InventoryListBTO.GetAllInventoryRecords());
}

void InventoryRecords_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    //e.NewItems will be an IList of all the items that were added in the AddRange method...
} 

【讨论】:

  • 第一行指出了我的问题。所以我在属性的设置器上添加了 CollectionChanged 事件处理程序。
【解决方案2】:

试试这个

public ObservableCollection<InventoryBTO> InventoryRecords
{
    get { return _inventoryRecords; }
    set 
    { 
      _inventoryRecords = value; 
      onPropertyChanged(this, "InventoryRecords");

    }
 }

public ObservableCollection<InventoryBTO> InventoryRecords
{
    get { return _inventoryRecords; }
    set 
    { 
      _inventoryRecords = value; 
      OnPropertyChanged(new PropertyChangedEventArgs("InventoryRecords"));

    }
 }

取决于您的实施。

【讨论】:

  • 无论何时为属性设置值,都会调用“onPropertyChanged”方法,进而引发 PropertyChanged 事件。
  • 我不明白。添加新的 InventoryBTO 时,PropertyChanged 事件如何影响集合的 CollectionChanged 事件?
  • 另外,您将如何访问包含已添加新项目的集合更改事件中的属性。
  • CollectionChanged 事件 在添加、删除、更改、移动项目或刷新整个列表时发生。 msdn.microsoft.com/en-us/library/ms653375.aspx
  • 正确,那么为什么当我将一个项目添加到可观察集合时,事件不会触发?我完全没有抓住重点吗?
猜你喜欢
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多