过滤ObserverableCollection仅显示某些项目 [英] Filtering ObserverableCollection to display only certain items

查看:695
本文介绍了过滤ObserverableCollection仅显示某些项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此链接: http://jacobmsaylor.com/?p=1270

但我有问题,试图对其进行调整

but i'm having problems with it, trying to make tweaks to it

<ListBox Name="PageList_ListBox" MouseDoubleClick="PageList_ListBox_OnMouseDoubleClick"
                             Background="#FFC9C9C9" Margin="0,5,0,0" ItemsSource="{Binding PageCollection, ElementName=This}">

public static ObservableCollection<MLBPage> _PageCollection = new ObservableCollection<MLBPage>();
public static ObservableCollection<MLBPage> PageCollection
        {
            get { return _PageCollection; }
        }

public ICollectionView _PageCollectionView { get; set; }

_PageCollectionView = CollectionViewSource.GetDefaultView(_PageCollection);

private bool FilterLeadersList(object item)
{
  MLBPage page = item as MLBPage;
  if (page.templateName.Contains("Leaders List"))
  {
    return true;
  }
  else
  {
    return false;
  }
}

我的MLBPage对象有2种类型... templateName可以是领导者列表或领导者头像。现在当我通过添加按钮过滤集合:

My MLBPage object has 2 types... where the "templateName" can be either "Leaders List" or "Leader Headshots".. now when I filter the collection by adding to a button:

_PageCollectionView.Filter = FilterLeadersList;

整个集合只是过滤器(绑定到列表框的_PageCollection变为空白),而不是只有包含名称中的领导者名单。

the whole collection just filters (the _PageCollection binded to a listbox turns blank) instead of only the items that contain "Leaders List" within the name....

有关如何修改此工作的任何帮助?

any help on how i can modify this to work?

推荐答案

将您的代码更改为:

 private ObservableCollection<MLBPage> _PageCollection = new ObservableCollection<MLBPage>();            
 public ICollectionView _PageCollectionView { get; set; }

只需执行一次(例如在ctor内)

just do this once (eg. within ctor)

 //ctor
 _PageCollectionView = CollectionViewSource.GetDefaultView(_PageCollection);
 _PageCollectionView.Filter = FilterLeadersList,

使用清除,添加,删除更改您的_PageCollection。

use clear, add , remove to alter your _PageCollection.

将您的列表框绑定到您的视图

bind your listbox to your view

<ListBox ItemsSource="{Binding _PageCollectionView}"/>

使用刷新刷新您的过滤器

use Refresh to refresh your filter

 _PageCollectionView.Refresh();

这篇关于过滤ObserverableCollection仅显示某些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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