CollectionViewで最近追加されたアイテムの位置またはアイテムを取得する方法はありますか?
Reagrds、Vikram
イベントを購読しCollectionView.CollectionChanged
ます。イベントが発生したら、のAction
プロパティを確認し、それが新しく追加されたアイテムとNotifyCollectionChangedEventArgs
等しい場合は、コレクションAdd
に含まれます。NewItems
通常、これには1つのアイテムのみが含まれ、適切な変数またはクラスメンバーに保存できます。最近追加されたアイテムが何であるかを知る必要がある場合は、この変数を読んでください。
に基づいて独自のコレクションを実装しますCollectionView
。このコレクション内に、アイテムとそれらが追加された時刻の間のマップを保存します(新しく追加されたアイテムがCollectionView.CollectionChanged
イベントにサブスクライブしていることを検出するため)。時間ごとにアイテムにアクセスするためのコレクション内のメソッドを定義しますpublic IEnumerable<T> GetItems(DateTime startTime, DateTime endTime)
。
から継承されるソースコレクションを作成します。INotifyCollectionChangedから暗黙的に継承するをINotifyCollectionChanged
使用できます。ObservableCollection
そして、ソースのCollectionChangedイベントをサブスクライブして、その中のAction
プロパティとNewItems
コレクションを確認できます。サンプルコード-
public ObservableCollection<object> Names
{
get;
set;
}
private ICollectionView source;
public ICollectionView Source
{
get
{
if (source == null)
{
source = CollectionViewSource.GetDefaultView(Names);
source.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(source_CollectionChanged);
}
return source;
}
}
void source_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
// Can play with e.NewItems here.
}
}