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;

名前に「Leaders List」を含むアイテムのみではなく、コレクション全体がフィルタリングされます (リストボックスにバインドされた _PageCollection が空白になります)。

これを機能するように変更する方法について何か助けはありますか?

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 に答える