CollectionView.filter 刷新视图时委托未触发

CollectionView.filter delegate is not firing when refreshing the view

我有 Observable 集合。我在集合更改事件中应用过滤器。这样我就可以在数据网格中过滤项目。 请在下面找到代码片段,

    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");
            };
        }
    }

输出:

我正在更新我的 viewModel 集合,然后刷新视图,

 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();
    }

但我的 view.filter 代表不是 invoked.hence 未能过滤集合。

我的看法:

<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>

请任何人帮助我在通过单击按钮刷新视图时触发过滤器委托。

谢谢

不要创建新的集合 - 你会得到一个不同的集合视图!该 collectionView 未连接到视图。

改为尝试清除集合:

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();
}