Answers for "wpf icollectionview filter"

C#
0

wpf icollectionview filter

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListBox ItemsSource={Binding Customers} />
</Window>

public class CustomerView
{
   public CustomerView()
   {
        DataContext = new CustomerViewModel();
   }
}
 
public class CustomerViewModel
{
    private ICollectionView _customerView;
 
    public ICollectionView Customers
    {
        get { return _customerView; }
    }
 
    public CustomerViewModel()
    {
        IList<Customer> customers = GetCustomers();
        _customerView = CollectionViewSource.GetDefaultView(customers);
    }
}
//The collection view adds support for selection tracking. 
//If you set the property IsSynchronizedWithCurrentItem to True 
//on the view that the collection is bound to, 
//it automatically synchronizes the current item of the CollectionView 
//and the View.
<ListBox ItemsSource="{Binding Customers}" IsSynchronizedWithCurrentItem="True" />

ICollectionView _customerView = CollectionViewSource.GetDefaultView(customers);
_customerView.Filter = CustomerFilter
 
 // filter
private bool CustomerFilter(object item)
{
    Customer customer = item as Customer;
    if(customer != null)
    	return customer.Name.Contains( _filterString );
    return false;
}

//If you change the filter criteria and you want to refresh the view, 
//you have to call Refresh() on the collection view
public string FilterString
{
  get { return _filterString; }
  set 
  { 
      _filterString = value; 
      NotifyPropertyChanged("FilterString");
      _customerView.Refresh();
  }
}

//Sorting data ascending or descending by one or multiple criterias 
//is a common requirement for viewing data. The collection view makes 
//it so easy to achieve this goal. Just add as many SortDescriptions 
//as you like to the CollectionView
ICollectionView _customerView = CollectionViewSource.GetDefaultView(customers);
_customerView.SortDescriptions.Add( 
                new SortDescription("LastName", ListSortDirection.Ascending );
_customerView.SortDescriptions.Add( 
                new SortDescription("FirstName", ListSortDirection.Ascending );

//The sorting technique explained above is really simple, but also quite 
//slow for a large amount of data, because it internally uses reflection. 
//But there is an alternative, more performant way to do sorting by 
//providing a custom sorter.

ListCollectionView _customerView = CollectionViewSource.GetDefaultView(customers);
                                         as ListCollectionView;
_customerView.CustomSort = new CustomerSorter(); 
 
public class CustomerSorter : IComparer
{
    public int Compare(object x, object y)
    {
        Customer custX = x as Customer;
        Customer custY = y as Customer;
        return custX.Name.CompareTo(custY.Name);
    }
}
Posted by: Guest on August-01-2020

C# Answers by Framework

Browse Popular Code Answers by Language