I made a sample to filter objects in ListBox, the groups hide when filter condition doesn't exist. It may give you some help.
XAML code:
<Window.DataContext>
<local:ViewModel></local:ViewModel>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="2">
<Label>Name:</Label>
<TextBox Width="100" Text="{Binding NameFilterStr,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
<ListBox Name="lbMain" Grid.Row="1" ItemsSource="{Binding StudentListView}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Width="150" />
<TextBlock Text="{Binding Age}"
Width="100" />
<TextBlock Text="{Binding Description}"
Width="100" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" ExpandDirection="Down">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Age:"></TextBlock>
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" />
<TextBlock Text="{Binding Path=ItemCount, StringFormat=Count:{0}}"
VerticalAlignment="Center"
Margin="5,0,0,0" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
</Grid>
C# code is:
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private List<Student> students = new List<Student>();
public List<Student> Students
{
get { return students; }
set { students = value; }
}
private string nameFilterStr;
public string NameFilterStr
{
get { return nameFilterStr; }
set
{
nameFilterStr = value;
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NameFilterStr"));
StudentListView.Refresh();
}
}
public ListCollectionView StudentListView { get; set; }
public ViewModel()
{
StudentListView = new ListCollectionView(Students);
Students.Add(new Student() { Name = "AA1", Age = 12, Description = "654646" });
Students.Add(new Student() { Name = "AA2", Age = 12, Description = "7967969" });
Students.Add(new Student() { Name = "AA3", Age = 13, Description = "11111123" });
Students.Add(new Student() { Name = "BB1", Age = 14, Description = "123131" });
Students.Add(new Student() { Name = "BB6", Age = 13, Description = "232525" });
Students.Add(new Student() { Name = "BB7", Age = 16, Description = "57474674" });
StudentListView.GroupDescriptions.Add(new PropertyGroupDescription("Age"));
StudentListView.Filter = Filter;
}
private bool Filter(object obj)
{
Student s = obj as Student;
if (s == null) return false;
if (string.IsNullOrEmpty(NameFilterStr))
{
return true;
}
else if (!string.IsNullOrEmpty(NameFilterStr))
{
if (s.Name.Contains(NameFilterStr))
{
return true;
}
return false;
}
else
{
if (s.Name.Contains(NameFilterStr) )
{
return true;
}
return false;
}
}
}
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Description { get; set; }
}
The Result picture is:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.