多个筛选器在 DataGrid WPF 中不起作用

本文关键字:WPF 不起作用 DataGrid 筛选 | 更新日期: 2023-09-27 18:36:11

我的数据绑定在我的 DataGrid 中工作正常,但是当我尝试从我的 CollectionViewSource 获取集合视图时,它是空的,并且我在 DataGrid 中的过滤器不起作用。 这是代码:

<Window.Resources>
    <CollectionViewSource x:Key="jobItemViewSource" d:DesignSource="{d:DesignInstance my:JobItem, CreateList=True}"/>
</Window.Resources>
<Grid Visibility="Visible" ShowGridLines="False" UseLayoutRounding="True" DataContext="{StaticResource jobItemViewSource}">
<DataGrid Grid.Row="4" Grid.ColumnSpan="6" LoadingRow="jobItemDataGrid_LoadingRow" ItemsSource="{Binding}" Name="jobItemDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" IsReadOnly="True" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" Grid.RowSpan="2">
                    <DataGrid.Columns>
                        <DataGridTextColumn x:Name="workOrderNoColumn" Binding="{Binding Path=WorkOrderNo}" Header="Work Order No." Width="75" />
                        <DataGridTextColumn x:Name="assigneeColumn" Binding="{Binding Path=PrimaryAssignee}" Header="Assignee" Width="80" />
                        <DataGridTextColumn x:Name="dateReceivedColumn" Binding="{Binding Path=DateReceived}" Header="Date Received" Width="90" />
                        <DataGridTextColumn x:Name="dueDateColumn" Binding="{Binding Path=DueDate}" Header="Due Date" Width="90" />
                        <DataGridTextColumn x:Name="statusColumn" Binding="{Binding Path=Status}" Header="Status" Width="70" />
                    </DataGrid.Columns>
                </DataGrid>
<CheckBox Content="Show Active Jobs" Name="actJobsCheckBox" Grid.Column="1" Grid.ColumnSpan="2"  Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="AddFiltering" Unchecked="RemoveFiltering"/>
                <CheckBox Content="Show Completed Jobs" Name="compJobsCheckBox" Grid.Column="3" Grid.ColumnSpan="3"  Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="AddFiltering" Unchecked="RemoveFiltering"/>
</Grid>

在我的代码隐藏中,我有以下内容:

public partial class AdminView : Window
{
    ObservableCollection <JobItem> JobItemList = new ObservableCollection<JobItem>();
    CollectionViewSource jobItemViewSource = new CollectionViewSource();
    ICollectionView view; 
    MainWindow parent;
    Employee currEmployee;
    GroupFilter filterList = new GroupFilter();
    public AdminView(MainWindow parent, Employee currEmployee)
    {
        this.parent = parent;
        this.currEmployee = currEmployee;
        InitializeComponent();
        jobItemDataGrid.DataContext = JobItemList;
        jobItemViewSource = (CollectionViewSource)(this.Resources[jobItemViewSource]);
        view = CollectionViewSource.GetDefaultView(jobItemDataGrid);

    }
private void AddFiltering(object sender, RoutedEventArgs args)
    {
        CheckBox check = sender as CheckBox;
        Predicate<object> active = new Predicate<object>(ContainsActive);
        Predicate<object> completed = new Predicate<object>(ContainsComp);
        switch (check.Name)
        {
            case "actJobsCheckBox":
                filterList.AddFilter(active);
                if( view != null)
                    view.Filter = filterList.Filter;
                break;
            case "compJobsCheckBox":
                filterList.AddFilter(completed);
                if (view != null)
                    view.Filter = filterList.Filter;
                break;
        }

    }
    private void RemoveFiltering(object sender, RoutedEventArgs args)
    {
        CheckBox check = sender as CheckBox;
        Predicate<object> active = new Predicate<object>(ContainsActive);
        Predicate<object> completed = new Predicate<object>(ContainsComp);
        switch (check.Name)
        {
            case "actJobsCheckBox":
                filterList.RemoveFilter(active);
                break;
            case "compJobsCheckBox":
                filterList.RemoveFilter(completed);
                break;
        }
    }
    public bool ContainsActive(object de)
    {
        JobItem item = de as JobItem;
        return (item.Status != "Completed");
    }
    public bool ContainsComp(object de)
    {
        JobItem item = de as JobItem;
        return (item.Status == "Completed");
    }

多个筛选器在 DataGrid WPF 中不起作用

我实际上已经找到了自己的答案,我只需要刷新我的观点,它就可以了。