gpt4 book ai didi

wpf - 在 WPF 中对 DataGrid 进行预排序

转载 作者:行者123 更新时间:2023-12-03 10:19:56 27 4
gpt4 key购买 nike

我有一个 DataGrid在具有多个列的 WPF 应用程序中,包括名称列。如果用户切换到特定 View ,我希望数据按名称预先排序(并且我希望排序箭头出现在名称标题中,就像用户单击了该标题一样)。但是,我找不到实现此目的的预期属性。我正在寻找类似 SortColumn 的东西, SortColumnIndex , SortDirection , 等等。

是否可以在标记 (XAML) 中指定默认排序列和方向,或者 WPF 工具包不支持这些列和方向 DataGrid ?

最佳答案

假设您在谈论 WPF Toolkit DataGrid 控件,您只需要设置 the CanUserSortColumns property为真,然后设置 the SortMemberPath property DataGrid 中的每个 DataGridColumn。

至于最初对集合进行排序,您必须使用 CollectionViewSource 并在其上设置排序,然后将其分配为 DataGrid 的 ItemsSource。如果您在 XAML 中执行此操作,那么它将非常简单:

<Window.Resources>
<CollectionViewSource x:Key="MyItemsViewSource" Source="{Binding MyItems}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="MyPropertyName"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>

<DataGrid ItemsSource="{StaticResource MyItemsViewSource}">

</DataGrid>

注意: “scm”命名空间前缀映射到 SortDescription 类所在的 System.ComponentModel。
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"

编辑:我认为有足够多的人从这篇文章中得到了帮助,这个被赞成的评论应该包含在这个答案中:

我不得不使用它来让它工作:
<DataGrid ItemsSource="{Binding Source={StaticResource MyItemsViewSource}}">

关于wpf - 在 WPF 中对 DataGrid 进行预排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1627149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com