2012-08-15 78 views
1

我正在研究綁定到ObservableCollection屬性的概念證明,該屬性返回委託的結果。委託還爲其設置了一個屬性,以便我可以更改使用的表達式,併爲集合觸發OnPropertyChanged。這允許我將ComboBox綁定到Collection,並且在更改表達式/查詢時,ComboBox中的可用選項也將更改。收藏/收藏查看基於Silverlight中的Linq查詢

代碼:

public delegate List<string> Del(); 

private Del _query; 
public Del Query 
{ 
    get 
    { 
     return _query; 
    } 
    set 
    { 
     _query= value; 
     OnPropertyChanged("BindList"); 
    } 
} 

private ObservableCollection<string> bindList; 
public ObservableCollection<string> BindList 
{ 
    get 
    { 
     var results = Query(); 
     bindList = new ObservableCollection<string>(results); 
     return bindList; 
    } 
    set 
    {//I believe I need this setter for the extra functionality provided by ObservableCollections over Lists 
     if(bindList != value) { 
      bindList = value; 
      OnPropertyChanged("BindList"); 
     } 
    } 
} 

由於這個工程,我想使一類出來的,這將是簡單結合。我正在尋求如何這樣做的指導。我想過一些關於子類化ObservableCollection,但是如何設置Items的問題。我還考慮過使用像IEnumerable和ICollectionView這樣的接口(以及通知接口)的自定義類。

總而言之,您將如何構建一個類來合併一個集合,該集合的成員基於委託查詢(關於子類/接口的具體LINQ)?

在此先感謝。

+0

'ICollectionView'看起來像是答案的一部分。它具有SourceCollection的只讀屬性,我認爲應該只在我的類中引用一個私有列表?除非我也從一些集合類繼承,當我嘗試綁定到我的類不需要綁定到'myCollection.SourceCollection'或綁定到'myCollection'就足夠了(myCollection是我的自定義類的類型)。我寧願不必做點語法,因爲我會有很多這些,它會變得混亂。 – Ethan 2012-08-16 14:41:57

回答

0

這是我到目前爲止所提出的。還沒有進行過很多測試,但到目前爲止它的外觀還不錯。

public class DynamicCollection<T> : IEnumerable, INotifyCollectionChanged 
{ 
    public ICollectionView Collection { get; private set; } 

    public delegate List<T> Del(); 

    private Del query; 
    public Del Query 
    { 
     get 
     { 
      return query; 
     } 
     set 
     { 
      if (query != value) 
      { 
       query = value;//set the new query 
       T currentItem = (T)Collection.CurrentItem;//save current item 
       Collection = new PagedCollectionView(Query());//recreate collection with new query 
       Collection.MoveCurrentTo(currentItem);//move current to the previous current (if it doesn't exist, nothing is selected) 
       OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));//Notify colleciton has changed. 
      } 
     } 
    } 

    public DynamicCollection() 
    { 
     Collection = new PagedCollectionView(new List<T>());//empty collection 
    } 

    public DynamicCollection(IEnumerable<T>collection) 
    { 
     Collection = new PagedCollectionView(collection); 
    } 

    public DynamicCollection(Del delegateQuery) 
    { 
     Query = delegateQuery; 
    } 

    #region IEnumerable Members 
    public IEnumerator GetEnumerator() 
    { 
     return Collection.GetEnumerator(); 
    } 
    #endregion IEnumerable Members 

    #region INotifyCollectionChanged Members 
    public event NotifyCollectionChangedEventHandler CollectionChanged; 
    protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    { 
     NotifyCollectionChangedEventHandler handler = CollectionChanged; 
     if (handler != null) 
     { 
      CollectionChanged(this, e); 
     } 
    } 
    #endregion INotifyCollectionChanged Members 
} 

它可以綁定在一個ComboBox使用ItemsSource="{Binding Path=myCollection, Mode=TwoWay}"來(給你DyamicCollection myCollection設置爲您的視圖模型/數據上下文的屬性)。

這一切都通過設置查詢,在我的情況下,我給一個LINQ到XML查詢返回一個列表。 Collection對此進行更新,綁定的ComboBox反映了此更新。

請隨時批評這一點。我相當肯定我已經有一些東西了,或者甚至有更好的方法來做到這一點。我願意接受反饋,並會隨着它的發展而更新這個答案。