我的应用程序中有一个数据网格和一个组合框。我必须过滤所选项目,我试图在我的应用程序中实现datagrid
事件,但我收到此错误。combobox
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>
输出窗口如下所示: