WPF CollectionViewSource 中自定义控件误用?
您应该在 WPF 自定义控件 的代码隐藏中使用 CollectionViewSource
吗?
我问是因为 CollectionViewSource.GetDefaultView(SOURCE)
与 .Filter
设置 更改视图,以便控件的所有实例具有相同的视图。
这是否意味着在自定义控件后面的代码中,您应该避免使用 CollectionViewSource ?
Should you use CollectionViewSource
in the WPF Custom Control's code behind?
I'am asking because the CollectionViewSource.GetDefaultView(SOURCE)
with .Filter
set
changes the view so that all instances of the Control have the same view.
Doesn't it mean, that in the Custom Control's code behind, you should avoid use of CollectionViewSource ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于所需的行为。例如,如果屏幕上有多个状态列表,并且想要同时按区域过滤所有状态,那么这可能是所需的行为。如果您不希望项目绑定到相同的过滤条件,您始终可以强制使用不同的视图源。
我认为避免使用 CollectionViewSource 直到您了解 ItemsSource -> 的交互集合视图->绑定项目控件可能是一个好主意,但我不知道我是否会说您通常应该避免它。
我认为可以合理地说,如果表单上的两个列表具有不同的过滤/排序要求,那么它们应该从不同的源属性绑定(即使这些属性是单个基础源的直接克隆)。这样,您仍然可以在 CollectionViewSource 级别应用过滤和存储,但以后不会产生意想不到的后果。
It depends on the desired behavior. E.g. if you had multiple lists of states on the screen and wanted to filter all of them by region at the same time, then this could be desired behavior. You can always force a different view source in cases where you do not want the items bound to the same filter criteria.
I think avoiding the use of CollectionViewSource until you understand the interactions of ItemsSource -> CollectionView -> Bound Item controls is probably a good idea, but I don't know that I would go so far as to say that you should avoid it in general.
I think it might be reasonable to say that if two lists on a form have different filtering/sorting requirements then they should be bound from different source properties (even if those properties are straight clones of a single underlying source). That way you can still apply filtering and storing at the CollectionViewSource level but without unintended consequences later on.