我的應用程序中有一個datagrid和一個組合框。我必須過濾combobox
所選項目上的datagrid
,我試圖在我的應用中實施CollectionViewSource.Filter
事件,但我得到此錯誤。'System.Windows.Data.BindingListCollectionView'視圖不支持篩選
我做了什麼:
代碼隱藏:
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>
輸出窗口看起來像這樣:
這段代碼適用於我,除了我用'新列表()'替換'db.LogEntries'。 db.LogEntries返回什麼? –
2013-04-04 13:32:24
它是對象 - 「公共對象集 LogEntries」。 LogEntry是我的表是我的數據庫,它被映射爲實體框架中的模型 –
user1221765
2013-04-04 13:36:09
也爲我工作 – user1064519 2013-04-04 13:43:07