Search code examples
wpficollectionview

CollectionView.filter delegate is not firing when refreshing the view


I have Observable collection. I'm applying filter in collection changed event. So that i'm getting filtered items in datagrid. Please find the code snippet below,

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
       vm.Customers.Add(new Customer() { Name = "Customer1", OrderName = "Order1" });
        vm.Customers.Add(new Customer() { Name = "Customer2", OrderName = "Order2" });
        vm.Customers.Add(new Customer() { Name = "Customer3", OrderName = "Order3" });
        vm.Customers.Add(new Customer() { Name = "Customer4", OrderName = "Order4" });
        vm.Customers.Add(new Customer() { Name = "Customer5", OrderName = "Order5" });
    }

     private void Customers_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ICollectionView _customerView = CollectionViewSource.GetDefaultView(vm.Customers);
        if(_customerView!=null)
        {
            _customerView.Filter = delegate (object item)
            {
                Customer customer = item as Customer;
                return customer.Name.Contains("Customer3");
            };
        }
    }

Output: After applying filter

I'm updating my viewModel collection and then i'm refreshing the view,

 private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        vm.Customers = new System.Collections.ObjectModel.ObservableCollection<Customer>();
        vm.Customers.Add(new Customer() { Name = "Customer1", OrderName = "1" });
        vm.Customers.Add(new Customer() { Name = "Customer2", OrderName = "2" });
        vm.Customers.Add(new Customer() { Name = "Customer3", OrderName = "3" });
        ICollectionView _customerView = CollectionViewSource.GetDefaultView(vm.Customers);
        _customerView.Refresh();
    }

but my view.filter delegate is not invoked.hence failed to filter the collection.

My View:

<UserControl x:Class="WpfApplication3.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WpfApplication3"

         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
    <local:UserControlViewModel/>
</UserControl.DataContext>
<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <DataGrid AutoGenerateColumns="True"  x:Name="list"  ItemsSource="{Binding Customers}"  />
    <Button Content="Add customers" Height="50" Width="100" Grid.Column="1" Click="Button_Click_2"/>
    <Button Content="Filter customer3" Height="50" Width="100" Grid.Column="2" Click="Button_Click"/>
    <Button Content="Add items" Height="50" Width="100" Grid.Column="3" Click="Button_Click_1"/>
</Grid>

Please anyone help me to fire Filter delegate when refreshing view through button click.

Thanks


Solution

  • don't create new collection - you will get a different collectionView for it! that collectionView is not connected to the view.

    try Clear collection instead:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        vm.Customers.Clear();
        vm.Customers.Add(new Customer() { Name = "Customer1", OrderName = "1" });
        vm.Customers.Add(new Customer() { Name = "Customer2", OrderName = "2" });
        vm.Customers.Add(new Customer() { Name = "Customer3", OrderName = "3" });
        ICollectionView _customerView = CollectionViewSource.GetDefaultView(vm.Customers);
        _customerView.Refresh();
    }