我正在将ComboBox绑定到实体,但我希望过滤数据。
迄今为止,我已经尝试了两种方法: 1. "简单"一点:通过LINQ直接将过滤器应用于ObjectSet to Entities 2. 设置过滤事件处理程序,如msdn中所描述的那样。
我对第一种方法感到满意,特别是因为生成到数据库的查询包含一个WHERE子句,因此不需要从远程数据库检索整个数据......
然而,如果在运行时我想更改所应用的过滤器,则第二种方法要灵活得多。我遵循了msdn上的示例,但我出现了异常,为什么?
所以,我的问题是: 1. 哪种方法更好? 2. 为什么我会出现异常?
这是我的代码:
迄今为止,我已经尝试了两种方法: 1. "简单"一点:通过LINQ直接将过滤器应用于ObjectSet to Entities 2. 设置过滤事件处理程序,如msdn中所描述的那样。
我对第一种方法感到满意,特别是因为生成到数据库的查询包含一个WHERE子句,因此不需要从远程数据库检索整个数据......
然而,如果在运行时我想更改所应用的过滤器,则第二种方法要灵活得多。我遵循了msdn上的示例,但我出现了异常,为什么?
所以,我的问题是: 1. 哪种方法更好? 2. 为什么我会出现异常?
这是我的代码:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//Do not load your data at design time.
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Load your data here and assign the result to the CollectionViewSource.
System.Windows.Data.CollectionViewSource myCollectionViewSource =
(System.Windows.Data.CollectionViewSource)
this.Resources["tSCHEDEViewSource"];
// If I use this I get the data filtered on startup, but is it the right mode?
//myCollectionViewSource.Source = _context.TSCHEDE.Where(s => s.KLINEA == kLinea && s.FCANC == "T").OrderBy(s => s.DSCHEDA).OrderByDescending(s => s.DSTORICO);
// Instead If I apply my custom filtering logic
myCollectionViewSource.Filter += new FilterEventHandler(filterSource);
myCollectionViewSource.Source = _context.TSCHEDE; // ... Here i get an exception:
// 'System.Windows.Data.BindingListCollectionView' view does not support filtering. ???
}
}
private void filterSource(object sender, FilterEventArgs e)
{
TSCHEDE scheda = e.Item as TSCHEDE;
if (scheda != null)
{
if (scheda.KLINEA == 990)
{
e.Accepted = true;
}
else
{
e.Accepted = false;
}
}
}
编辑: 我已经尝试在视图上实现Filter属性而不是设置EventHandler:
myCollectionView = (BindingListCollectionView)myCollectionViewSource.View;
myCollectionView.Filter = new Predicate<object>(Contains);
public bool Contains(object de)
{
TSCHEDE scheda = de as TSCHEDE;
return (scheda.KLINEA == 990);
}
现在我得到一个不太有用的异常:
System.NotSupportedException: 指定的方法不受支持。 at System.Windows.Data.CollectionView.set_Filter(Predicate`1 value)
编辑
XAML代码:
<UserControl.Resources>
<CollectionViewSource x:Key="tSCHEDEViewSource" d:DesignSource="{d:DesignInstance my:TSCHEDE, CreateList=True}" >
</CollectionViewSource>
<DataTemplate x:Key="SchedaTemplate">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=KSCHEDA}" Width="60"></TextBlock>
<TextBlock Text="{Binding Path=DArticolo}" Width="200"></TextBlock>
<TextBlock Text=" - " Width="40"></TextBlock>
<TextBlock Text="{Binding Path=DSTORICO}" Width="150"></TextBlock>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid Background="PapayaWhip" DataContext="{StaticResource tSCHEDEViewSource}" DataContextChanged="StartHere" Name="rootGrid">
<ComboBox ItemTemplate="{StaticResource SchedaTemplate}" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="23,129,0,0" Name="tSCHEDEComboBox1" SelectedValuePath="KSCHEDA" VerticalAlignment="Top" Width="393">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</Grid>
现在我认为问题出在XAML绑定上,而不是代码后台...