1

我的应用程序中有一个数据网格和一个组合框。我必须过滤所选项目,我试图在我的应用程序中实现datagrid事件,但我收到此错误。comboboxCollectionViewSource.Filter

'System.Windows.Data.BindingListCollectionView' 视图不支持过滤

我做了什么 :

代码隐藏:

 private CollectionViewSource logViewSource;
    IEnumerable<LogEntry> Query;
    DB_Entities db = new DB_Entities();

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.Query = db.LogEntries;
        this.logViewSource=(CollectionViewSource)(FindResource("logEntriesViewSource"));
        logViewSource.Source = this.Query;
    }

    private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
    {
        LogEntry log = e.Item as LogEntry;
        ComboBoxItem cbi = (ComboBoxItem)comboBox1.SelectedItem;

        if (cbi != null)
        {
            string s = cbi.Content.ToString();
            if (log.Type == s)  
            {
                e.Accepted = true;
            }
            else
            {
                e.Accepted = false;
            }
        }

    }

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        CollectionViewSource cvs = FindResource("logEntriesViewSource") as CollectionViewSource;
        cvs.View.Refresh();
    }

XAML:

<Window.Resources>
    <CollectionViewSource x:Key="logEntriesViewSource" d:DesignSource="{d:DesignInstance my:LogEntry, CreateList=True}" Filter="CollectionViewSource_Filter" />
</Window.Resources>

<Grid DataContext="{StaticResource logEntriesViewSource}">

    <ComboBox Height="29" HorizontalAlignment="Left" SelectionChanged="comboBox1_SelectionChanged"
              Name="comboBox1" VerticalAlignment="Top" Width="115" Margin="388,2,0,0">
        <ComboBoxItem Content="Warning"/>
        <ComboBoxItem Content="Error"/>
        <ComboBoxItem Content="Exception"/>            
    </ComboBox>

    <DataGrid AutoGenerateColumns="False" 
              EnableRowVirtualization="True" Height="274" HorizontalAlignment="Left" ItemsSource="{Binding}" 
              Name="logEntriesDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="503" Margin="0,37,0,0">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="iDColumn" Binding="{Binding Path=ID}" Header="ID" Width="auto" />
            <DataGridTextColumn x:Name="fileNameColumn" Binding="{Binding Path=FileName}" Header="File Name" Width="auto" />
            <DataGridTextColumn x:Name="typeColumn" Binding="{Binding Path=Type}" Header="Type" Width="auto" />
            <DataGridTextColumn x:Name="criticalColumn" Binding="{Binding Path=Critical}" Header="Critical" Width="auto" />
        </DataGrid.Columns>
    </DataGrid>       
</Grid>

输出窗口如下所示:

输出

4

2 回答 2

2

ObjectSet<T>派生自 ObjectQuery,它实现了 WPF 用来生成可绑定集合的 IListSource。由于您指定的是接口,而不是实现,因为您的可绑定集合 WPF 正在生成(或者可能是强制转换?)您的集合到 BindingListCollectionView。您的 CollectionViewSource 将过滤委托给它的底层集合 ObjectSet(被解释为 BindingListCollectionView),它不支持过滤

我相信解决方案是将您的 db.Query 集合包装在支持过滤的东西中——可能是 List<T> 或 ObservableCollection<T>。也许是这样的:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.Query = new ObservableCollection<LogEntry>(db.LogEntries);
    this.logViewSource=(CollectionViewSource)(FindResource("logEntriesViewSource"));
    logViewSource.Source = this.Query;
}
于 2013-04-04T18:44:22.873 回答
1

这是我试过的代码:

   CollectionViewSource logViewSource;
   IEnumerable<object> Query;
   IEnumerable<string> objects= new List<string> { "a", "b" };
   Query = objects;
   logViewSource=(CollectionViewSource)(FindResource("logEntriesViewSource"));
   logViewSource.Source = Query;
于 2013-04-04T14:10:14.817 回答