WPF DataGrid默认排序不起作用

编程入门 行业动态 更新时间:2024-10-27 15:28:23
本文介绍了WPF DataGrid默认排序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个DataGrid,列XAML这样:

I have a DataGrid, with columns XAML as such:

<DataGridTextColumn Header="Time" Binding="{Binding Date, StringFormat='yyyy-MM-dd HH:mm:ss'}" SortMemberPath="Date" SortDirection="Descending" Width="130" CanUserResize="True" /> <DataGridTextColumn Header="Level" Binding="{Binding Level}" Width="60" CanUserResize="True" /> <DataGridTextColumn Header="Source" Binding="{Binding Logger}" Width="150" CanUserResize="True" /> <DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*" CanUserResize="True" />

我将其绑定到一个 ObservableCollection< EalsLogEvent> ,其中 EalsLogEvent.Date 键入 DateTime :

I bind this to an ObservableCollection<EalsLogEvent>, where EalsLogEvent.Date is typed DateTime:

public ObservableCollection<EalsLogEvent> LogEvents { get { return _logEvents; } }

网格视图模式使用定时器自行刷新,一切似乎都很好,网格,除了它首次加载,在应用程序启动。然后, Time 列似乎按照降序排序,但按升序排序。

The grid viewmodel uses a timer to refresh itself, and everything seems fine with the grid, except when it first loads, on app startup. Then, the Time column appears to be sorted descending, but is sorted ascending.

要获得排序权,我必须单击列标题两次;第一次将订单更改为升序,现在与列的内容匹配。第二次点击列标题将其排序顺序更改为降序,此时它会正确排序列内容,即降序。

To get the sort right, I must click the column header twice; the first time changes the order to ascending, which now matches the content of the column. The second click on the column header changes its sort order back to descending, and this time it sorts the column contents properly, i.e. descending.

如果我使用LINQ订购当 _logEvents 被刷新时,收集,我失去了用户通过单击其标题为列设置的任何订单。如果我有这个观点告诉模型LINQ排序应该使用哪个顺序,那么一些东西会闻起来很糟糕。

If I use LINQ to order the collection when _logEvents gets refreshed, I lose whichever order the user had set for the column by clicking its header. If I have to have the view tell the model which order the LINQ sort should use, something smells bad.

推荐答案

你可以使用您的XAML中的 CollectionViewSource 来定义默认排序。

You could use a CollectionViewSource in your XAML to define the default sorting.

假设我们有一个视图模型:

Assuming we have a view model:

public class ViewModel : INotifyPropertyChanged { public ObservableCollection<Item> Items { get; private set; } }

我们可以创建一个自定义的 CollectionView 项目集合:

We can create a custom CollectionView for the Items collection:

<Window xmlns:l="clr-namespace:YourNamespace" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"> <Window.DataContext> <l:ViewModel/> </Window.DataContext> <Window.Resources> <CollectionViewSource Source="{Binding Items}" x:Key="GridItems"> <CollectionViewSource.SortDescriptions> <scm:SortDescription PropertyName="Date" Direction="Descending"/> </CollectionViewSource.SortDescriptions> </CollectionViewSource> </Window.Resources> <DataGrid ItemsSource="{Binding Source={StaticResource GridItems}}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Time" Binding="{Binding Date, StringFormat='yyyy-MM-dd HH:mm:ss'}" Width="130" CanUserResize="True" /> <DataGridTextColumn Header="Level" Binding="{Binding Level}" Width="60" CanUserResize="True" /> <DataGridTextColumn Header="Source" Binding="{Binding Logger}" Width="150" CanUserResize="True" /> <DataGridTextColumn Header="Message" Binding="{Binding Message}" Width="*" CanUserResize="True" /> </DataGrid.Columns> </DataGrid> </Window>

使用这种方法,您的基础源集合(项目

With this approach, your underlying source collection (Items in this example) will not be affected, the sorting occurs only in the view.

正如你可以在 MSDN :

您可以将集合视图作为绑定源集合顶部的图层,允许您根据排序,过滤器导航和显示集合,和组查询,所有没有必须操纵底层的源集合本身。如果源集合实现INotifyCollectionChanged接口,将由CollectionChanged事件引发的更改传播到视图。

You can think of a collection view as the layer on top of the binding source collection that allows you to navigate and display the collection based on sort, filter, and group queries, all without having to manipulate the underlying source collection itself. If the source collection implements the INotifyCollectionChanged interface, the changes raised by the CollectionChanged event are propagated to the views.

您还应该注意以下事项:

You should also note the following:

所有集合都有默认的CollectionView。 WPF始终绑定到视图而不是集合。如果直接绑定到一个集合, WPF实际上绑定到该集合的默认视图。

All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection.

所以,使用 CollectionViewSource ,您只需为您的收藏定义自定义视图。

So, using the CollectionViewSource, you're just defining a custom view for your collection.

更多推荐

WPF DataGrid默认排序不起作用

本文发布于:2023-11-10 06:01:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574541.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   WPF   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!