筛选ObserverableCollection以仅显示某些项
本文关键字:显示 ObserverableCollection 筛选 | 更新日期: 2023-09-27 17:51:15
我一直在关注这个链接:http://jacobmsaylor.com/?p=1270
但是我遇到了问题,试图对它进行调整
<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"可以是"领导者列表"或"领导者头像"。现在,当我通过添加一个按钮来过滤集合时:
_PageCollectionView.Filter = FilterLeadersList;
整个集合只是过滤器(_PageCollection绑定到一个列表框变成空白),而不是只有包含"Leaders List"在名称....
有什么帮助我可以修改这个工作吗?
把你的代码改成:
private ObservableCollection<MLBPage> _PageCollection = new ObservableCollection<MLBPage>();
public ICollectionView _PageCollectionView { get; set; }
只做一次。在男星)
//ctor
_PageCollectionView = CollectionViewSource.GetDefaultView(_PageCollection);
_PageCollectionView.Filter = FilterLeadersList,
使用clear, add, remove来修改_PageCollection。
将列表框绑定到视图
<ListBox ItemsSource="{Binding _PageCollectionView}"/>
使用Refresh来刷新过滤器
_PageCollectionView.Refresh();