'System.Windows.Data.BindingListCollectionView

2019-08-07 13:17发布

问题:

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;