[英]ObservableCollection and CollectionChanged Event
為什么在下面的代碼中沒有觸發 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)
{
}
問題是您正在將您的私有成員分配給您從方法中返回的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...
}
嘗試這個
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"));
}
}
取決於您的實施。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.