2013-04-04 52 views
1

我的應用程序中有一個datagrid和一個組合框。我必須過濾combobox所選項目上的datagrid,我試圖在我的應用中實施CollectionViewSource.Filter事件,但我得到此錯誤。'System.Windows.Data.BindingListCollectionView'視圖不支持篩選

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

我做了什麼:

代碼隱藏:

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

+0

這段代碼適用於我,除了我用'新列表()'替換'db.LogEntries'。 db.LogEntries返回什麼? – 2013-04-04 13:32:24

+0

它是對象 - 「公共對象集 LogEntries」。 LogEntry是我的表是我的數據庫,它被映射爲實體框架中的模型 – user1221765 2013-04-04 13:36:09

+0

也爲我工作 – user1064519 2013-04-04 13:43:07

回答

2

ObjectSet<T>從派生的ObjectQuery它實現IListSource其中WPF使用,以產生可綁定集合。既然你指定了一個接口,而不是一個實現,因爲你的可綁定集合WPF正在生成(或者可能將其轉換爲?)你的集合到一個BindingListCollectionView。您的CollectionViewSource將過濾委託給它的底層集合ObjectSet(它被解釋爲BindingListCollectionView),其中doesn't support filtering

我認爲,解決辦法是換你db.Query收藏的東西,不支持過濾 - 也許列表<牛逼>或一個ObservableCollection <牛逼>。也許是這樣的:

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

的確如此。你救了我從這個問題.. – user1221765 2013-04-05 09:02:37

+0

感謝解決我的問題;) – 2017-04-26 07:32:59

1

這是我試圖的代碼:

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

我從數據庫中獲取datagrid的數據,通過db.LogEntries,db是實體的對象,而LogEntries是Object Set – user1221765 2013-04-04 14:56:48