我無法將項目添加到方法內部的Observable集合。如何添加項目到Observable集合,這是一個方法內?
下面是代碼:
- 我會打電話給GetFeaturelist()方法首先,這裏返回觀察的集合,然後我會打電話的AddRange應該將項目添加到裏面GetFeaturelist FeatureList()方法,它是不會發生。
請幫我解決這個問題。
GetFeaturelist().AddRange(_featureListBuffer);
private ObservableCollection<Feature> GetFeaturelist()
{
return FeatureList;
}
public class ObservableCollection<T> : System.Collections.ObjectModel.ObservableCollection<T>
{
/// <summary>
/// Adds the elements of the specified collection to the end of the ObservableCollection.
/// </summary>
public void AddRange(IEnumerable<T> collection)
{
foreach(T i in collection)
{
Items.Add(i);
}
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
注:
- 我能做到這樣
FeatureList.AddRange(_featureListBuffer);
其工作正常,但我想這樣GetFeaturelist().AddRange(_featureListBuffer);
「......沒有發生」 - 我無法重現您的問題。該代碼適用於我。也許你可以添加更多的細節? *什麼*不*發生? - 而且*發生了什麼? – 2013-02-27 12:29:29
從GetFeatureslist()返回的'ObservableCollection'是__your__可觀察集合還是.NET的可觀察集合? – Alex 2013-02-27 12:30:26
@Alex:有點令人困惑,但是您可以看到一個新類「ObservableCollection」是通過從BCL'ObservableCollection '(在不同的命名空間中)派生而定義的。 –
2013-02-27 12:32:36