Silverlightで作業しています。ObservableCollectionをファイリングしたいと思います。ObservableCollectionのCollectionViewをSilverlightで作成する方法
私はSilverlightにCollectionViewSourceがなく、メソッドとイベントの量が不足しているため、ICollectionViewに注目し始めました。私はしばらく検索して、誰かがICollectionViewの実装のサンプルコードを持っているのだろうか?
Silverlightで作業しています。ObservableCollectionをファイリングしたいと思います。ObservableCollectionのCollectionViewをSilverlightで作成する方法
私はSilverlightにCollectionViewSourceがなく、メソッドとイベントの量が不足しているため、ICollectionViewに注目し始めました。私はしばらく検索して、誰かがICollectionViewの実装のサンプルコードを持っているのだろうか?
残念ながら、ICollectionViewはSilverlight 2.0のDataGridにのみ使用され、唯一の実装はSystem.Windows.Controls.Dataの内部にあるListCollectionViewです。
DataGridにバインドしていない場合、ICollectionViewはデータコントロールアセンブリで定義されているため、基本コントロール(リストボックスなど)では使用できないため、あまり知らせてくれません。コアではない。
これはWPFとかなり大きな違いです。
しかし、あなたの質問のポイントに、DataGridを含むアセンブリには、それがどのように行われているかを知りたい場合に役立つ実装があります。最悪の場合、リフレクターはあなたの友人です...
ObservableCollectionへのデータバインディングを行う場合、1つの方法はバリューコンバーターを使用することです。
別の方法は、(下に(実装方法UpdateFilteredStores)を参照)、このようなビューモデルのプロパティに基づいてフィルタリングを行うことになるビューモデルCLRオブジェクトにLINQを使用することであろう。
namespace UnitTests
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
public class ViewModel : INotifyPropertyChanged
{
private string name;
public ViewModel()
{
this.Stores = new ObservableCollection<string>();
this.Stores.CollectionChanged += new NotifyCollectionChangedEventHandler(this.Stores_CollectionChanged);
// TODO: Add code to retreive the stores collection
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public ObservableCollection<string> Stores { get; private set; }
public IEnumerable<string> FilteredStores { get; private set; }
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
this.UpdateFilteredStores();
}
}
private void Stores_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
this.UpdateFilteredStores();
}
private void UpdateFilteredStores()
{
this.FilteredStores = from store in this.Stores
where store.Contains(this.Name)
select store;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("FilteredStores"));
}
}
}
}
ViewModel CLRオブジェクトにLINQの例がありますか? –
サンプルコードを使って自分の応答を更新しました。 –
'Name'が変更されるたびに' UpdateFilteredStores'を呼び出す必要はなく、Linqは自動的にリクエストを再評価します。 – Grokwik
CollectionViewSource Silverlight 3で利用可能になりました。これについての良い記事をチェックしてくださいhere。
がSilverlightで利用可能> = 3 –