Grouping and searching text in ListView in WPF

431 Views Asked by At

I am going to develop a view for messages history like in skype. I want to provide grouping and searching i.e. all message of same date should be in one group and also user can search for text/message in history. Both functionality is available in WinForms ListView, but I am using WPF ListView.

Help me how to do this. Tell me if i need to replace ListView with any other element.

1

There are 1 best solutions below

1
On

As for grouping, you could use GroupDescriptions of the CollectionViewSource like this:

<CollectionViewSource x:Key="myListViewSource" Source="{Binding DataSource}" >

    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="GroupPropertyName"  />
    </CollectionViewSource.GroupDescriptions>        

</CollectionViewSource>

<ListBox ItemsSource="{Binding Source={StaticResource myListViewSource}}" />

As For filtering:

Add textbox:

<TextBox Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" Width="100" />

View model:

private ICollectionView _view;
private string _filter;

public ObservableCollection<string> DataSource { get; private set; }

public string Filter 
{
   get
   {
      return filter;
   }
   set
   {
      if (value != filter)
      {
         filter = value;             
         RaisePropertyChanged("Filter");
         _view.Refresh();
      }
   }
}

public MyViewModel()
{
   DataSource = new ObservableCollection<string>();
   _view = CollectionViewSource.GetDefaultView(DataSource);
   _view.Filter = o => String.IsNullOrEmpty(Filter) ? true : ((string)o).Contains(Filter); 
}