- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在将 ComboBox 绑定(bind)到实体,但我想过滤数据。
到目前为止我尝试了两种方式:
我对第一种方法很满意,最重要的是因为对数据库生成的查询包含一个 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)中,而不是代码隐藏...
最佳答案
检查这个
过滤需要一个委托(delegate)(谓词),过滤将基于该委托(delegate)(谓词)发生。 Predicate 根据它返回的值 true 或 false 接受项目,它选择或取消选择一个元素。
this.Source.Filter = item => {
ViewItem vitem = item as ViewItem;
return vItem != null && vitem.Name.Contains("A");
};
关于c# - CollectionViewSource,如何过滤数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497506/
我是 WPF 新手,尝试使用自定义排序对 CollectionViewSource 进行排序时遇到了困难。情况如下: 我有一个使用参数调用的 SearchView,它变成了它的 datacontext
tldr:CollectionViewSource.Filter 被另一个控件覆盖。我怎样才能有两层过滤,以便控件只能看到预过滤的集合? 我有一个第三方网格控件,我通过 ICollectionView
谁能告诉我如何在 Windows 8 上的 XAML 中设置 CollectionViewSource 的正确属性? x:name 是对象的名称。来源是什么? d:Source 和 Source 有
我正在使用 CollectionViewSource 对我的数据进行分组。在我的数据中,我需要对 Property1 和 Property2 进行分组。 唯一的规定是我不想要另一个组的子组。因此,当我
我正在尝试设置一个从 CollectionViewSource 获取数据的 ListBox。我想要发生的是,当我更新基础数据源时,列表框也会更新。我的 Xaml 看起来像这样......
我有一个 collectionviewsource 项目。每个项目都是一个对象,它有一个属性 SentTime 和另一个属性 ParentId。我想按 ParentId 对项目进行分组,并按以下方式对
我正在尝试向 CollectionViewSource 添加附加行为,以便我可以在 XAML 中的 View 模型上提供过滤器 Predicate 属性。 XAML 如下所示:
我想使用过滤器 将ComboBox 绑定(bind)到我的数据。为此,我创建了一个 TextBox 和一个 ComboBox。在后面的代码中,我读取了一个文件并生成了类 Channel 的对象,这些对
我正在将 ItemsControl 绑定(bind)到 CollectionViewSource。这是代码: this.Trucks = new ObservableCollection();
我正在将 ComboBox 绑定(bind)到实体,但我想过滤数据。 到目前为止我尝试了两种方式: “简单”一:直接将过滤器应用到 ObjectSet throughtLINQ 到实体 按照上文所述设
我正在尝试对 CollectionViewSource 进行基本使用,但我肯定遗漏了一些东西,因为它无法正常工作。这是我的 XAML:
我有一个需要过滤的“东西”列表,然后以分组和排序的方式显示。计算分组和排序顺序所需的数据不能作为简单的属性提供 - 需要在代码中完成一些工作来计算顺序和组。 CollectionViewSource
我有一个绑定(bind)到 CollectionViewSource 的 WPF ListView。它的来源绑定(bind)到一个属性,如果用户选择一个选项,它可以改变。 当 ListView 源由于
我真的很大ObservableCollection我需要对其提供用户友好的过滤。 public static async Task RefilterViewAsync(this ItemsContro
我编写了一个带有搜索扩展名的自定义 WPF 控件,我们将其命名为 MyControl . 控件是 ItemsControl 的后代类(class)。 所以我像这样将数据源提供给它: 控件本身使用 pr
我处于这种困境中,希望有人能帮助我 抱歉,我无法在此处粘贴代码作为在此处发布公司栏。 我试图在xaml中使用collectionviewsource。我尝试了两种方法,静态资源和cvs.source。
我在 DataGrid 中使用 CollectionViewSource 作为 ItemSource mLevelSource; public ObservableCollection LevelS
一段时间以来,我一直试图在我的数据网格中对数据进行分组,但没有成功。在我的 ViewModel 中,属性: public ObservableCollection Competitors { get;
我正在使用 MVVM 模式开发 WPF 桌面应用程序。 我试图根据 TextBox 中输入的文本从 ListView 中过滤出一些项目。我希望在更改文本时过滤 ListView 项目。 我想知道当过滤
我想将一个datagrid.itemssource绑定(bind)到一个匿名类型的列表,所以我将它绑定(bind)到一个collectionViewSource,但我需要在列表中添加或删除一个项目,但
我是一名优秀的程序员,十分优秀!