将 Wpf HierarchicalDataTemplate ItemsSource 绑定到字典中的 CollectionViewSource?
我正在尝试显示一个 Wpf Treeview,其中包含按 CollectionViewSource 排序的项目。
目前,除了在我的资源字典中使用此代码进行排序之外,一切正常:
<HierarchicalDataTemplate DataType="{x:Type books:Container}" ItemsSource="{Binding Path=Items}">
<nav:ContainerControl />
</HierarchicalDataTemplate>
更改 HierarchicalDataTemplate 以绑定到 CollectionViewSource(反过来从 Items 属性中提取)的语法是什么?
我尝试了Bea Stollnitz 博客上发布的代码的变体,但没有成功。我不知道如何设置 CollectionViewSource 的源。
I'm trying to display a Wpf Treeview with items sorted by a CollectionViewSource.
Currently, everything is working except sorting using this code in my resource dictionary:
<HierarchicalDataTemplate DataType="{x:Type books:Container}" ItemsSource="{Binding Path=Items}">
<nav:ContainerControl />
</HierarchicalDataTemplate>
What would be the syntax for changing the HierarchicalDataTemplate to bind to a CollectionViewSource that in turn pulls from the Items property?
I've tried variations of the code posted on Bea Stollnitz's blog with no success. I can't figure out how to set the source of the CollectionViewSource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我只是说我讨厌我提出的解决方案,但它确实有效。也许 WPF 大师会为我们俩提供更好的选择。当然,如果您在视图后面使用 ViewModel,则只需在 ViewModel 中使用 CollectionView 包装模型的 Items 属性即可完成操作。
但这里有另一个解决方案。基本上,您的 HierarchicalDataTemplate 可以保持原样,除非您将转换器添加到绑定中。我实现了以下转换器并相应地更改了 XAML。
CollectionViewConverter.cs
Well let me just say that I hate my proposed solution, but it does work. Perhaps a WPF guru will enlighten us both with a better alternative. Of course if you were using a ViewModel behind your view, you could simply wrap the Items property of the model with a CollectionView in the ViewModel and be done with it.
But here's another solution. Basically, your HierarchicalDataTemplate can stay as is except you would add a Converter to the Binding. I implemented the following converter and changed the XAML accordingly.
CollectionViewConverter.cs
我按照你的建议做了,并用 ListCollectionView 包装了 Items 集合:
我错过了什么吗?
I did as you suggested and wrapped the Items collection with a ListCollectionView:
Did I miss anything?