ObservableCollection和CollectionChanged事件

时间:2010-06-15 02:32:01

标签: c# observablecollection

为什么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)
    {

    } 

2 个答案:

答案 0 :(得分:10)

问题在于,您是将私有成员分配给您从方法中获取的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...
} 

答案 1 :(得分:0)

试试这个

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

    }
 }

OR

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

    }
 }

取决于您的实施。