2

Is there a way I can filter the CollectionViewSource to only show games in the ItemsSource which "Title" contains the "searchString"?

In my PosterView I have this CVS:

<CollectionViewSource x:Key="GameListCVS"
                      Source="{Binding PosterView}"
                      Filter="GameSearchFilter">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Title" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

and also this ItemsControl

<ItemsControl x:Name="gameListView"
              ItemsSource="{Binding Source={StaticResource GameListCVS}}">

My MainWindow.xaml contains the search box which can successfully pass the searchString (string containing what is in the search box) to PosterView.

PosterView binding is actually (confusingly, I know), an ObservableCollection

public ObservableCollection<GameList> PosterView { get; set; }

And here is how games are added to the ObservableCollection

games.Add(new GameList
{
    Title = columns[0],
    Genre = columns[1],
    Path = columns[2],
    Link = columns[3],
    Icon = columns[4],
    Poster = columns[5],
    Banner = columns[6],
    Guid = columns[7]
});
Pete Kemp
  • 91
  • 1
  • 8

1 Answers1

7

If you are creating the CollectionViewSource in the view, you should filter it there as well:

private void GameSearchFilter(object sender, FilterEventArgs e)
{
    GameList game = e.Item as GameList;
    e.Accepted = game != null && game.Title?.Contains(txtSearchString.Text);
}

The other option would be to bind to an ICollectionView and filter this one in the view model:

_view = CollectionViewSource.GetDefaultView(sourceCollection);
_view.Filter = (obj) => 
{
    GameList game = obj as GameList;
    return game != null && game.Title?.Contains(_searchString);
};
...
public string SearchString
{
    ...
    set { _searchString = value; _view.Refresh(); }
}

Or sort the source collection itself directly.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • In the first solution (code-behind PosterView.xaml.cs), where would i pull "txtSearchString.Text", or how can I pass in searchString from another method in PosterView.xaml.cs? As the search box is in MainWindow.xaml, I need to get the searchString into this method, right? – Pete Kemp Oct 24 '18 at 14:37
  • @PeteKemp: From the search box named "txtSearchString". You have a search box in the view, haven't you? – mm8 Oct 24 '18 at 14:38
  • Sorry, the MainWindow.xaml has the search box, I can get the variable into a method in PosterView though. The search box is in the "header bar" of the solution – Pete Kemp Oct 24 '18 at 14:40
  • So what's the relationship between the MainWindow and the PosterView? What is PosterView? – mm8 Oct 24 '18 at 14:41
  • mainwindow is a container for different usercontrols, posterview, bannerview, and listview – Pete Kemp Oct 24 '18 at 14:43
  • So obviously you need to pass the string to the PosterView somehow. You may for example use a shared DataContext but this is a separate issue really. – mm8 Oct 24 '18 at 14:45