1

我的数据绑定在我的 DataGrid 中工作正常,但是当我尝试从我的 CollectionViewSource 获取 collectionview 时,它为空,并且我在 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");
    }
4

1 回答 1

0

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

于 2012-06-04T20:08:30.097 回答