如何使用 Xaml 中的 SortDescriptions 对 TreeView 项目进行排序?
我有一个绑定到 TreeView
的 Layers
列表,其中每个实例都有一个 Effects
列表。我通过 HierarchicalDataTemplate 展示它们,该模板效果很好,但我尝试使用 SortDescriptions
对它们进行排序。
我不知道如何在 xaml 中执行此操作,但这样做只会对第一级项目进行排序,而不是子项目:
ICollectionView view = CollectionViewSource.GetDefaultView ( treeView1.ItemsSource );
view.SortDescriptions.Add ( new SortDescription ( "Name", ListSortDirection.Ascending ) );
我尝试首先按 .Color
对它们进行排序,然后按 对它们进行排序.名称
。
有什么想法吗?
编辑:我添加了这段代码:
<Window.Resources>
<CollectionViewSource x:Key="SortedLayers" Source="{Binding AllLayers}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Color" />
<scm:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
但这仍然只适用于层次结构的第一级。如何为每个layer.Effects集合指定它?
I have a list of Layers
binded to a TreeView
where each instance has a list of Effects
. I show them via a HierarchicalDataTemplate which works great but I am trying to sort them using SortDescriptions
.
I don't know how to do this in xaml but doing this sorts only the first level of items, not the sub items:
ICollectionView view = CollectionViewSource.GetDefaultView ( treeView1.ItemsSource );
view.SortDescriptions.Add ( new SortDescription ( "Name", ListSortDirection.Ascending ) );
I am trying to sort them first by .Color
, then by .Name
.
Any ideas?
EDIT: I added this code:
<Window.Resources>
<CollectionViewSource x:Key="SortedLayers" Source="{Binding AllLayers}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Color" />
<scm:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
But this still only does it for the first level of hierarchy. How can I specify it for each layer.Effects collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议使用转换器对子项目进行排序。
像这样的东西:
和转换器:
I would suggest to use converter to sort sub items.
Something like this:
and converter:
我发现使用多转换器更好:
I find using a multi converter is better:
这不是基于 XAML 的解决方案,但我遇到了同样的问题并找到了如下所示的解决方案,
我假设您有如下 3 个类:AllLayers、Layers 和 Layers。效果
之后,您现有的绑定应该可以使用排序。无需更改 XAML 或其他任何内容。
This is not the XAML based solution, but I was facing the same problem and found the solution like below,
I am assuming you have 3 classes like below: AllLayers, Layers & Effects
After that, your existing binding should work with sorting. No need of change to your XAML or anything.