2

我在这里关注这个链接: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”可以是“Leaders List”或“Leader Headshots”......现在当我通过添加到按钮来过滤集合时:

_PageCollectionView.Filter = FilterLeadersList;

整个集合只是过滤器(绑定到列表框的 _PageCollection 变为空白),而不仅仅是名称中包含“Leaders List”的项目......

关于如何修改它以使其工作的任何帮助?

4

1 回答 1

3

将您的代码更改为:

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

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

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

使用 clear、add、remove 来改变你的 _PageCollection。

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

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

使用 Refresh 刷新您的过滤器

 _PageCollectionView.Refresh();
于 2014-03-20T18:46:17.153 回答