'System.Windows.Data.BindingListCollectionView' view does not support filtering

荒凉一梦 提交于 2019-12-11 11:27:44

问题


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 :


回答1:


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;
}



回答2:


this is the code i tried :

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


来源:https://stackoverflow.com/questions/15811347/system-windows-data-bindinglistcollectionview-view-does-not-support-filtering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!