WPF ICollectionView 筛选器重置
我有一个从 ObservableCollection
派生的 CollectionView
:
private static ObservableCollection<CalculationViewModel> _calculations;
CalculationViewModelsCollection = (CollectionView)CollectionViewSource.GetDefaultView(_calculations);
我的问题是,当过滤器的结果是什么时,我想清除过滤器,然后重新过滤与其他条件,但 CollectionView
始终为空。
我尝试通过以下方式重置过滤器:
CalculationViewModelsCollection.Filter = null;
CalculationViewModelsCollection.Refresh();
但是
CalculationViewModelsCollection.Filter = delegate(object p)
{
return true;
};
它们都不起作用。
您能否提供一些如何重置 CollectionView
上的过滤器的建议?
I have a CollectionView
derived from an ObservableCollection
:
private static ObservableCollection<CalculationViewModel> _calculations;
CalculationViewModelsCollection = (CollectionView)CollectionViewSource.GetDefaultView(_calculations);
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();
and
CalculationViewModelsCollection.Filter = delegate(object p)
{
return true;
};
But none of them worked.
Could you give some advice how to reset a filter on a CollectionView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的示例来看,我不完全确定您如何获取 CollectionView,也不确定我是否正确理解您的问题。
但无论如何,我希望下面的示例代码可以帮助您解决问题。它是一个具有包含字符串的列表框和“过滤器”文本框的应用程序。如果列表中没有任何内容与过滤器匹配,则过滤器将设置为空,从而显示所有项目。
XAML:
隐藏代码:
From your example, I'm not entirely sure how you're getting your CollectionView, nor am I sure I understand your question correctly.
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:
Code-behind:
我犯了一个很大的绑定错误。我根本不明白它是如何工作的。
所以问题是,重置过滤器很简单,只需将值设置为 null 即可。
还有一件事。我尝试像您一样创建 ListCollectionView 。
但过滤器不起作用,我无法将 SortDescription 添加到 CollectionView 中。
我以这种方式创建 CollectionView:
一切正常。但理想情况下,你的技术也必须有效。
I did a big binding mistake. I don't understand how it works at all.
So the matter is that, it's simple to reset a filter, just set the value to null.
There is one more thing. I tried to create ListCollectionView like you did it.
But the filter didn't work, and I couldn't add SortDescription to the CollectionView.
I create CollectionView this way:
and everything work fine. But ideally your technique have to work too.