WPF ICollectionView筛选器重置 [英] WPF ICollectionView Filter Reset
问题描述
我有一个 CollectionView
源自 ObservableCollection
:
private static ObservableCollection<CalculationViewModel> _calculations;
CalculationViewModelsCollection = (CollectionView)CollectionViewSource.GetDefaultView(_calculations);
我的问题是,当过滤器的结果为空时,我想清除过滤器,并在其他条件下重新过滤,但 CollectionView
始终为空。
My problem is that, when the result of the filter is nothing, I'd like to clear the filter, and re-filter with other conditions, but the CollectionView
is always empty.
我尝试重置过滤这些方式:
I tried to reset the filter these ways:
CalculationViewModelsCollection.Filter = null;
CalculationViewModelsCollection.Refresh();
和
CalculationViewModelsCollection.Filter = delegate(object p)
{
return true;
};
但它们都不起作用。
您能否提供一些建议,如何在 CollectionView
上重置过滤器?
Could you give some advice how to reset a filter on a CollectionView
?
推荐答案
从您的示例中,我不能完全确定您如何获取CollectionView,也不能确定我正确理解了您的问题。
From your example, I'm not entirely sure how you're getting your CollectionView, nor am I sure I understand your question correctly.
但是无论如何,我希望下面的示例代码可以帮助您解决问题。这是一个具有包含字符串的列表框和过滤器文本框的应用。如果列表中没有与过滤器匹配的内容,则过滤器将设置为null,从而显示所有项目。
But anyway, I hope the sample code below helps you with your problem. It's an app that has a listbox containing strings, and a "filter" textbox. if nothing in the list matches the filter, the filter will be set to null and thus display all items.
XAML:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBox x:Name="textBox" TextChanged="TextBox_TextChanged"/>
<ListBox x:Name="listBox"/>
</StackPanel>
</Window>
代码隐藏:
public partial class MainWindow : Window
{
ListCollectionView lcv;
Predicate<object> filterFx;
public MainWindow()
{
InitializeComponent();
ObservableCollection<string> s = new ObservableCollection<string>();
"The Quick Brown Fox Jumps Over The Lazy Dog"
.Split(' ')
.ToList()
.ForEach((word) => s.Add(word.ToString()));
this.lcv = new ListCollectionView(s);
this.listBox.ItemsSource = this.lcv;
this.filterFx = (p) => ((string)p).ToUpper().Contains(this.textBox.Text.ToUpper());
lcv.Filter = this.filterFx;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
lcv.Refresh();
if (lcv.Count == 0)
lcv.Filter = null;
else
lcv.Filter = filterFx;
}
}
这篇关于WPF ICollectionView筛选器重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!