BindingListCollectionView

BindingListCollectionView

本文介绍了'System.Windows.Data.BindingListCollectionView'视图不支持过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个数据网格和一个组合框。我必须过滤 comboxbox 所选项目上的 datagrid ,我尝试实现派生从ObjectQuery中实现,该对象实现WPF用来生成可绑定集合的IListSource。由于您是在指定接口而不是实现,因此WPF会在您的可绑定集合中生成(或强制转换?)您的集合到BindingListCollectionView。您的CollectionViewSource正在将过滤委派给其基础集合ObjectSet(该集合被解释为BindingListCollectionView),该对象不支持过滤



我相信解决方案是将db.Query集合包装在支持过滤的内容中-也许是List< T>或ObservableCollection T。也许是这样的:

  private void Window_Loaded(对象发送者,RoutedEventArgs e)
{
此。查询=新的ObservableCollection< LogEntry>(db.LogEntries);
this.logViewSource =(CollectionViewSource)(FindResource( logEntriesViewSource));;
logViewSource.Source = this.Query;
}


I have a datagrid and a combobox in my application. I have to filter the datagrid on combobox selected item, I tried to implement the CollectionViewSource.Filter Event in my app, but I get this error.

what I have done :

Code-behind :

 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>

Output window looks like this :

解决方案

ObjectSet<T> derives from ObjectQuery which implements IListSource which WPF uses to generate bindable collections. Since you're specifying an interface, not an implementation, as your bindable collection WPF is generating (or maybe casting?) your collection to a BindingListCollectionView. Your CollectionViewSource is delegating the filtering to it's underlying collection the ObjectSet (which is being interpreted as a BindingListCollectionView) which doesn't support filtering.

I believe the solution is to wrap your db.Query collection in something that does support filtering - maybe a List<T> or an ObservableCollection<T>. Maybe something like this:

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

这篇关于'System.Windows.Data.BindingListCollectionView'视图不支持过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:25