gpt4 book ai didi

c# - CollectionViewSource,如何过​​滤数据?

转载 作者:太空狗 更新时间:2023-10-29 22:12:20 25 4
gpt4 key购买 nike

我正在将 ComboBox 绑定(bind)到实体,但我想过滤数据。

到目前为止我尝试了两种方式:

  • “简单”一:直接将过滤器应用到 ObjectSet throughtLINQ 到实体
  • 按照上文所述设置过滤事件处理程序 msdn

我对第一种方法很满意,最重要的是因为对数据库生成的查询包含一个 WHERE 子句,所以不必从远程数据库检索所有数据....

但是,#2 方法要灵活得多,如果在运行时我想更改应用的过滤...我已经按照 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;
}
}
}

编辑:我尝试在 View 上实现 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: Specified method is not supported. 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 绑定(bind)中,而不是代码隐藏...

最佳答案

检查这个

1) CollectionView Filtering

过滤需要一个委托(delegate)(谓词),过滤将基于该委托(delegate)(谓词)发生。 Predicate 根据它返回的值 true 或 false 接受项目,它选择或取消选择一个元素。

this.Source.Filter = item => {
ViewItem vitem = item as ViewItem;
return vItem != null && vitem.Name.Contains("A");
};

2) FIltering the data Dynamically

关于c# - CollectionViewSource,如何过​​滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com