我的应用程序有一个过滤器功能,它使用复选框来过滤掉某些绑定到数据网格的可观察集合项。我根本没有收到错误,过滤器的功能不像我想要的那样。这是它的 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;
}
}
}