© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
21 replies
Rodonies

WPF CollectionView Filter on Property of Model?

in a MVVM WPF app, how would I go about filtering a ListBox that is bound to a property of a model?

//Model
class Y {
  string Name;
  List<X> ListOfX;
}

class X {
  string Name;
}
//Model
class Y {
  string Name;
  List<X> ListOfX;
}

class X {
  string Name;
}


//ViewModel
public ObservableCollection<Y> ObservableListOfY { get; set; }


private Y _SelectedY;
public Y SelectedY;
{
  get { return _SelectedY; }
  set
  {
    _SelectedY = value;
    OnPropertyChanged();
  }
}

private string _SearchBoxText = string.Empty;
public string SearchBoxText
{
  get { return SearchBoxText; }
  set
  {
    _SearchBoxText = value;
    FilterData();
    OnPropertyChanged();
  }
}

public ctor()
{
  ObservableListOfY = ...
}

public void FilterData()
{
  //This filter works
  CollectionViewSource.GetDefaultView(ObservableListOfY ).Filter = o => (o as Y).ListOfX.Any(x => x.Name.Contains(SearchBoxText));

  //This one doesn't, probably because ListOfZ is not an ObservableCollection
  CollectionViewSource.GetDefaultView(SelectedY.ListOfZ).Filter = o => (o as X).Name.Contains(SearchBoxText);
}
//ViewModel
public ObservableCollection<Y> ObservableListOfY { get; set; }


private Y _SelectedY;
public Y SelectedY;
{
  get { return _SelectedY; }
  set
  {
    _SelectedY = value;
    OnPropertyChanged();
  }
}

private string _SearchBoxText = string.Empty;
public string SearchBoxText
{
  get { return SearchBoxText; }
  set
  {
    _SearchBoxText = value;
    FilterData();
    OnPropertyChanged();
  }
}

public ctor()
{
  ObservableListOfY = ...
}

public void FilterData()
{
  //This filter works
  CollectionViewSource.GetDefaultView(ObservableListOfY ).Filter = o => (o as Y).ListOfX.Any(x => x.Name.Contains(SearchBoxText));

  //This one doesn't, probably because ListOfZ is not an ObservableCollection
  CollectionViewSource.GetDefaultView(SelectedY.ListOfZ).Filter = o => (o as X).Name.Contains(SearchBoxText);
}


<!--View-->
<TextBox Text="{Binding SearchBoxText}" />
<ListBox ItemsSource="{Binding ObservableListOfY}" SelectedItem="{Binding SelectedY}" />
<ListBox ItemsSource="{Binding SelectedY.ListOfX}" />
<!--View-->
<TextBox Text="{Binding SearchBoxText}" />
<ListBox ItemsSource="{Binding ObservableListOfY}" SelectedItem="{Binding SelectedY}" />
<ListBox ItemsSource="{Binding SelectedY.ListOfX}" />




I want to filter out all the Y's that don't have a single X that matches the SearchText, I achieve this by setting the Filter property on YCollectionView in the ViewModel.

The problem is that even though that works, if you select a Y in the listbox it'll show every X, even if they don't match the text.

I can't set the Filter property on a CollectionView because ListOfX is a property of the Y model and I don't think I should use a CollectionView in the model, how would you normally achieve this behaviour?

is creating a ObservableListOfX and updating it in the setter of SelectedY a valid solution?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ What is the purpose of `ModelBindingContext.Model`, `ModelName`, and `ModelBinderAttribute.Name`?
C#CC# / help
3y ago
✅ Invoking an event on property changed (WPF)
C#CC# / help
17mo ago
❔ CollectionView Questions
C#CC# / help
3y ago
❔ WPF binding expression of property from ItemsControl item with viewmodel property
C#CC# / help
4y ago