2009-03-22 4 views
8

Silverlightで作業しています。ObservableCollectionをファイリングしたいと思います。ObservableCollectionのCollectionViewをSilverlightで作成する方法

私はSilverlightにCollectionViewSourceがなく、メソッドとイベントの量が不足しているため、ICollectionViewに注目し始めました。私はしばらく検索して、誰かがICollectionViewの実装のサンプルコードを持っているのだろうか?

答えて

1

残念ながら、ICollectionViewはSilverlight 2.0のDataGridにのみ使用され、唯一の実装はSystem.Windows.Controls.Dataの内部にあるListCollectionViewです。

DataGridにバインドしていない場合、ICollectionViewはデータコントロールアセンブリで定義されているため、基本コントロール(リストボックスなど)では使用できないため、あまり知らせてくれません。コアではない。

これはWPFとかなり大きな違いです。

しかし、あなたの質問のポイントに、DataGridを含むアセンブリには、それがどのように行われているかを知りたい場合に役立つ実装があります。最悪の場合、リフレクターはあなたの友人です...

+3

がSilverlightで利用可能> = 3 –

1

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")); 
      } 
     } 
    } 
} 
+0

ViewModel CLRオブジェクトにLINQの例がありますか? –

+0

サンプルコードを使って自分の応答を更新しました。 –

+1

'Name'が変更されるたびに' UpdateFilteredStores'を呼び出す必要はなく、Linqは自動的にリクエストを再評価します。 – Grokwik

6

CollectionViewSource Silverlight 3で利用可能になりました。これについての良い記事をチェックしてくださいhere

関連する問題