仅在 XAML 中更改列表视图排序属性/方向
我有一个简单的 ListView
并希望按数字或字母顺序、升序或降序对内容进行排序。选择来自下拉框。我知道我可以使用 CollectionViewSource
来实现排序,但如何动态更改 SortDescription 或方向?
更新:
好的,我已经像这样设置了 CVS,viewModel 是 ListView 当前绑定的内容。我要求将 PropertyName 绑定到当前选定的组合框项目的属性 PropertyName
。组合框绑定到一个自定义列表,该列表公开我想要排序的属性名称。
它抱怨我尝试使用的 PropertyName:
无法在类型的“PropertyName”属性上设置“Binding” '排序描述'。只能在 DependencyProperty 上设置“Binding” DependencyObject 的一个。
<CollectionViewSource Source="{StaticResource viewModel.ListValues}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="{Binding Path=SortPropertyName, Source=comboSort}"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" />
I have a simple ListView
and want to sort the contents numerically or alphabetically, ascending or descending. The choice comes from a drop down box. I understand I can use CollectionViewSource
to achieve the sorting but how can I alter the SortDescription or direction on the fly?
Update:
Ok so I have setup my CVS like so, the viewModel is what the ListView is currently bound to. I require the PropertyName to be bound to the currently selected combo box item's property PropertyName
. The combo box is bound to a custom list that expose the propertyname on which I want to sort.
It complains about the PropertyName that im attempting to use:
A 'Binding' cannot be set on the 'PropertyName' property of type
'SortDescription'. A 'Binding' can only be set on a DependencyProperty
of a DependencyObject.
<CollectionViewSource Source="{StaticResource viewModel.ListValues}" x:Key="cvs">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="{Binding Path=SortPropertyName, Source=comboSort}"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以在你的视图模型的代码后面
编辑
这一切,这是你的视图模型的一个小例子,
一个小视图模型
希望这有帮助
you can this all at code behind in your viewmodel
EDIT
here is a little example for your view model
a little viewmodel
hope this helps
您还可以将其放入行为中,添加另一个要绑定的属性以动态设置排序描述方向,但此解决方案仅适用于按一个属性排序。它当然可以扩展到更多用途。
XAML:
行为:
You can also put this into a behavior, adding another property to bind to to dynamically set the sort description direction, but this solution only works for sorting by one property. It could certainly be expanded to work for more.
XAML:
Behavior: