刷新 ObjectDataProvider 时视图丢失
我正在基于 Josh Smith 的示例在列表上实现文本框过滤器 http://joshsmithonwpf.wordpress.com/2007/06/12/searching-for-items-in-a-listbox。 基本上,它将视图上的过滤器设置为检查搜索框中文本的委托。 我像这样连接过滤器:
var pickerView = FindResource("sortedRulesView") as CollectionViewSource;
new TextSearchFilter(pickerView.View, SearchTextBox);
稍后,当我刷新 ObjectDataProvider 时,过滤器丢失了。 我注意到刷新后 pickerView.View 有不同的哈希码。 数据刷新时是否会重新创建所有视图? 这是否意味着每当我调用 ObjectDataProvider.Refresh() 时我都应该重新连接过滤器? 有没有一些更智能的方法来安装这个过滤器,而不需要保姆?
I'm implementing a textbox filter on a list based on Josh Smith's example at http://joshsmithonwpf.wordpress.com/2007/06/12/searching-for-items-in-a-listbox. Basically, it sets the Filter on the view to a delegate that checks against the text in the search box. I hook up the filter like so:
var pickerView = FindResource("sortedRulesView") as CollectionViewSource;
new TextSearchFilter(pickerView.View, SearchTextBox);
Later, when I refresh the ObjectDataProvider, the filter is lost. I've noticed that pickerView.View has a different hashcode after the refresh. Are all the views recreated when the data refreshes? Does that mean I should reattach the filter again whenever I call ObjectDataProvider.Refresh()? Is there some smarter way to install this filter that wouldn't require babysitting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说得对,当设置 CollectionViewSource.Source 时, CollectionViewSource.View 将被替换。
解决方案是使用 CollectionViewSource.Filter< /a> 事件而不是 CollectionView.Filter 属性。 当你的视图消失时,它会一直存在。
您只需对 Josh Smith 的 TextSearchFilter 类:
您的连接代码将变为:
You're right in saying that CollectionViewSource.View will be replaced when CollectionViewSource.Source is set.
The solution is to use the CollectionViewSource.Filter event instead of the CollectionView.Filter property. This will stick around when your View goes away.
You can do this with minimal changes to Josh Smith's TextSearchFilter class:
Your hookup code then becomes: