0

我的应用程序有一个过滤器功能,它使用复选框来过滤掉某些绑定到数据网格的可观察集合项。我根本没有收到错误,过滤器的功能不像我想要的那样。这是它的 xaml 代码:

<Window.Resources>
    <CollectionViewSource x:Key="jobItemViewSource" d:DesignSource="{d:DesignInstance my:JobItem, CreateList=True}"/>
</Window.Resources>

<!--
 Irrelevant Content
  -->

<CheckBox Content="Show Active Jobs" Name="actJobsCheckBox" Grid.Column="1" Grid.ColumnSpan="2"  Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="AddAFiltering" Unchecked="RemoveAFiltering" />
                <CheckBox Content="Show Completed Jobs" Name="compJobsCheckBox" Grid.Column="3" Grid.ColumnSpan="3"  Grid.Row="3" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="AddCFiltering" Unchecked="RemoveCFiltering"/>

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

然后这是我为后面的代码编写的内容:

public AdminView(Employee currEmployee)
    {
        this.Title = "Administrator View - " + currEmployee.Name;
        InitializeComponent();
        jobItemDataGrid.DataContext = JobItemList;
        jobItemViewSource = (CollectionViewSource)(this.Resources["jobItemViewSource"]);

        LoadDummies();


    }

private void AddAFiltering(object sender, RoutedEventArgs args)
    {
        jobItemViewSource.Filter += new FilterEventHandler(ActiveJobs_Filter);
    }

    private void RemoveAFiltering(object sender, RoutedEventArgs args)
    {
        jobItemViewSource.Filter -= new FilterEventHandler(ActiveJobs_Filter);
    }

    private void AddCFiltering(object sender, RoutedEventArgs args)
    {
        jobItemViewSource.Filter += new FilterEventHandler(CompletedJobs_Filter);
    }

    private void RemoveCFiltering(object sender, RoutedEventArgs args)
    {
        jobItemViewSource.Filter -= new FilterEventHandler(CompletedJobs_Filter);
    }

    private void ActiveJobs_Filter(object sender, FilterEventArgs e)
    {
        JobItem currJob = e.Item as JobItem;
        if (currJob != null)
        {
            if (!currJob.Status.Equals("Completed"))
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }
    }

    private void CompletedJobs_Filter(object sender, FilterEventArgs e)
    {
        JobItem currJob = e.Item as JobItem;
        if (currJob != null)
        {
            if (currJob.Status.Equals("Completed"))
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }
    }
4

1 回答 1

1

我不确定如何使用您的方法修复它。但这里是我将如何完成这项任务。创建一个名为 FilteredJobItemList 的属性以绑定到而不是主列表。将您的复选框绑定到属性 IsActiveChecked/IsCompletedChecked。在这些设置器中,为您的 FilteredJobItemList 引发 propertychanged 事件。

public ObservableCollection<JobItem> FilteredJobItemList
{
   get
   {
      if ((IsActiveChecked) && (!IsCompletedChecked))
      {
          return JobItemList.Where(x => x.status != "Completed");
      }
      else if ((!IsActiveChecked) && (IsCompletedChecked))
      {
          return JobItemList.Where(x => x.status == "Completed");
      }
      else
      {
          return JobItemList;
      }
   }
}

public bool IsActiveChecked
{
   get
   {
      return _isActiveChecked;
   }
   set
   {
      if (value != _isActiveChecked)
      {
          _isActiveChecked = value;
          OnPropertyChanged("IsActiveChecked");
          OnPropertyChanged("FilteredJobItemList");
      }
   }
} 

public bool IsCompletedChecked
{
   get
   {
      return _isCompletedChecked;
   }
   set
   {
      if (value != _isCompletedChecked)
      {
          _isCompletedChecked = value;
          OnPropertyChanged("IsCompletedChecked");
          OnPropertyChanged("FilteredJobItemList");
      }
   }
} 
于 2012-05-29T15:24:17.887 回答