2010-06-15 246 views
5

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

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) 
    { 

    } 
+1

看起来像你马上挂接到集合中的构造函数,然后用完全不同的集合覆盖引用。也许你反而意味着要做一个AddRange而不是覆盖?无论哪种方式,只要将值设置为属性,您创建并随后连接的可观察集合就会消失 – 2010-06-15 03:33:28

回答

10

问题是,您正在将您的私有成员分配给您从方法中获得的ObservableCollection的新实例。因此,发生的事情是,你连接到一个集合的事件,然后吹掉那个实例,并用一个你从来没有联系过事件处理程序的新实例替换它。这是你可以做的。创建一个从ObservableCollection继承并增加了一个的AddRange方法的类:

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

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

    } 

    protected override void OnCollectionChanged(System.Collections.Specialized.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... 
} 
+0

第一行指出我的问题。所以我在属性的setter上添加了CollectionChanged事件处理程序。 – 2017-05-11 08:44:48

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")); 

    } 
} 

根据您的实现。

+0

,将调用方法“onPropertyChanged”,然后引发PropertyChanged事件。 – VoodooChild 2010-06-15 02:40:11

+0

我不明白。在添加新的InventoryBTO时,PropertyChanged事件如何影响集合的CollectionChanged事件? – user337816 2010-06-15 02:48:34

+0

另外,您将如何访问包含添加的新项目的collection changed事件中的属性。 – user337816 2010-06-15 02:57:02

相关问题