2

I was following this link here: 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;
  }
}

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;

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?

user1189352
  • 3,628
  • 12
  • 50
  • 90
  • to the first guy that helped me so much and deleted your answer.. repost it and i'll give you credit. it was my fault. your code probably worked i just messed up as i needed to compare templateType and not templateName – user1189352 Mar 20 '14 at 19:00

1 Answers1

3

change your code into:

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

just do this once (eg. within ctor)

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

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();
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • btw your original binding dont work because _PageCollection is not a public PROPERTY – blindmeis Mar 20 '14 at 18:48
  • blindmeis edited OP. just didn't want to clutter it with too much so that people actually read the post =[ – user1189352 Mar 20 '14 at 18:49
  • hm it's weird.. so i followed your instructions.. it filters literally everything (listbox is just empty). my listbox turns up blank when i run the program. but if i comment out "_PageCollectionView.Filter = FilterLeadersList;" the whole playlist comes up.. so i'm not sure.. – user1189352 Mar 20 '14 at 18:55
  • i just double checked to make sure a template name in there is NOT leaders list.. sigh i just don't know why it wont work – user1189352 Mar 20 '14 at 18:56
  • wait think i know why... brb – user1189352 Mar 20 '14 at 18:59
  • it worked.. my fault. i was using templateName instead of templateType... thank you – user1189352 Mar 20 '14 at 18:59