带有ObservableCollection< T>的Silverlight ListBox和动态过滤器 [英] Silverlight ListBox with ObservableCollection<T> and dynamic filter

查看:112
本文介绍了带有ObservableCollection< T>的Silverlight ListBox和动态过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有这个类:

  public class MyData 
{
public bool IsActive { get; set;}
public String Data1 {get; set;}
public String Data2 {get; set;}
}

  ObservableCollection< MyData> data = new ObservableCollection< MyData> ;; 
ListBox.ItemsSource = data;

按照预期将项目添加到ObservableCollectionworks;但是,我想确保我的列表框仅显示 IsActive 设置为true的项目 - 因为这样,我无法使用Linq查询来设置ItemsSource它不是一个ObservableCollection,它的IEnumerable并没有对列表框进行任何更新通知。

解决方案

你的答案是CollectionViewSource。而不是绑定到列表,绑定到一个CollectionViewSource的实例。



稍微退化的例子如下(我不知道你是否使用ViewModels,Locators等解决您的数据和列表。)



假设在您的标记中,您的资源中声明了一个CollectionViewSource,如下所示:

 < phone:PhoneApplicationPage.Resources> 
< CollectionViewSource x:Key =src/>
< / phone:PhoneApplicationPage.Resources>

然后,您的列表绑定如下所示:

 < ListBox x:Name =MyListBoxItemsSource ={Binding Source = {StaticResource src}}> 

最后,在代码中,您可以结婚列表和收藏视图源:

  var collectionView = this.Resources [src]作为CollectionViewSource; 
//检查null等等
collectionView.Source = observableCollectionThatIAmBindingTo;
collectionView.View.Filter = new Predicate< Object>(o =>((ItemType)o).IsActive);

此外,您可能需要查看Bea Stollnitz关于该主题的文章:



http://bea.stollnitz.com/blog /?p = 31



http://bea.stollnitz.com/blog/?p=392


Lets say I have this class:

public class MyData
{
    public bool IsActive{get;set;}
    public String Data1 {get;set;}
    public String Data2 {get;set;}
}

and an

ObservableCollection<MyData> data = new ObservableCollection<MyData>;
ListBox.ItemsSource = data;

Adding items to the ObservableCollectionworks as expected; however, I want to make sure that my listbox only displays items where IsActive is set to 'true' -- I can't use a Linq query to set the ItemsSource because then its not an ObservableCollection, its IEnumerable and does not do any update notifications to the listbox.

解决方案

Your answer is CollectionViewSource. Instead of binding to the list, bind to an instance of CollectionViewSource.

A slightly degenerate example follows (I am not sure if you're using ViewModels, Locators, etc to resolve your data and your list.)

Assume in your markup you have a CollectionViewSource declared in your resources as follows:

<phone:PhoneApplicationPage.Resources>
    <CollectionViewSource x:Key="src"/>
</phone:PhoneApplicationPage.Resources>

Then your list binding looks like:

<ListBox x:Name="MyListBox" ItemsSource="{Binding Source={StaticResource src}}">

Finally, in code you can marry your list and your collection view source:

        var collectionView = this.Resources["src"] as CollectionViewSource;
        // Check for null, etc.
        collectionView.Source = observableCollectionThatIAmBindingTo;
        collectionView.View.Filter=new Predicate<Object>(o => ((ItemType)o).IsActive );

Additionally, you may want to check out Bea Stollnitz' articles on the topic at:

http://bea.stollnitz.com/blog/?p=31

http://bea.stollnitz.com/blog/?p=392

这篇关于带有ObservableCollection&lt; T&gt;的Silverlight ListBox和动态过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆