“System.Windows.Data.BindingListCollectionView”视图不

2019-10-17 23:24发布

我有一个DataGrid和我的应用程序的组合框。 我有过滤datagridcombobox选择的项目,我试图实现CollectionViewSource.Filter事件在我的应用程序,但我得到这个错误。

我做了什么 :

代码隐藏:

 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>

输出窗口如下所示:

Answer 1:

对象集<T>从的ObjectQuery它实现IListSource其中WPF使用,以产生可绑定集合导出。 既然你指定一个接口,而不是实现,因为你可绑定集合WPF正在生成(或者铸造?)你收集到BindingListCollectionView。 你CollectionViewSource被委派筛选到它的底层集合的对象集(这被解释为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;
}


Answer 2:

这是我试过的代码:

   CollectionViewSource logViewSource;
   IEnumerable<object> Query;
   IEnumerable<string> objects= new List<string> { "a", "b" };
   Query = objects;
   logViewSource=(CollectionViewSource)(FindResource("logEntriesViewSource"));
   logViewSource.Source = Query;


文章来源: 'System.Windows.Data.BindingListCollectionView' view does not support filtering