要實現的接口是INotifyCollectionChanged。參數此事件(NotifyCollectionChangedEventArgs)可以報告說,已經改變了項目的批次 - 此接口與WPF通過CollectionChanged事件綁定通信。
這樣做的一種方法是實現自己的MyBatchObservableColleciton,派生或(更好)嵌入列表(或任何其他適當的集合)。實現方法來修改innter集合並記錄所有這些更改。當您準備將所有更改提交到WPF時,只需發送更改列表。這裏是你可以做什麼(僅適用於實施「添加」 funcitonality到您的收藏)的一個子集例如:
class BatchObservableColleciton<T> : INotifyCollectionChanged, IEnumerable
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
private List<T> _list;
private List<T> _addedItems;
public BatchObservableColleciton() {
_list = new List<T>();
_addedItems = new List<T>();
}
public IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
public void Add(T item)
{
_list.Add(item);
_addedItems.Add(item);
}
public void commit() {
if(CollectionChanged != null) {
CollectionChanged(this, new NotifyCollectionChangedEventArgs(
NotifyCollectionChangedAction.Add, _addedItems));
}
_addedItems.Clear();
}
}
從來沒有嘗試過自己,但我認爲這是一段路要走。 Google for Custom & ObservableCollection。雖然不是確切的,但在那裏幾乎沒有實現。
來源
2012-02-16 08:29:14
Uri
這是錯誤的答案。除IEnumerable之外,您必須實現INotifyCollectionChanged – Uri 2012-02-16 08:30:33
不,它會起作用,只需將每個添加/刪除的整個ChartDataCollection屬性重置爲圖表數據源 – 2012-02-27 15:33:45
@Uri INotifyCollectionChanged顯示OP所具有的問題。儘管您的解決方案有效,但它架構過度。我的解決方案有效,而且更簡單。 – 2012-02-27 21:51:12