2012-08-15 49 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反映了此更新。

请随时批评这一点。我相当肯定我已经有一些东西了,或者甚至有更好的方法来做到这一点。我愿意接受反馈,并会随着它的发展而更新这个答案。