有界数据更改后重新排序 WPF DataGrid [英] Re-sort WPF DataGrid after bounded Data has changed

查看:19
本文介绍了有界数据更改后重新排序 WPF DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在基础数据更改重新排序我的DataGrid的方法.

I am looking for a way to re-sort my DataGrid when the underlying data has changed.

(设置很标准:DataGrid 的ItemSource 属性绑定到一个ObservableCollection; 列是DataGridTextColumns; 里面的数据DataGrid 对 ObservableCollection 内部的变化做出正确反应;用鼠标单击时排序工作正常)

(The setting is quite standard: The DataGrid's ItemSource property is bound to an ObservableCollection; The columns are DataGridTextColumns; The data inside the DataGrid reacts correctly on changes inside the ObservableCollection; Sorting works fine when clicked with the mouse)

有什么想法吗?

推荐答案

我花了一整个下午,但我终于找到了一个解决方案,令人惊讶的是简单简短高效:

It took me the whole afternoon but I finally found a solution that is surprisingly simple, short and efficient:

要控制所讨论的 UI 控件的行为(这里是 DataGrid),可以简单地使用 CollectionViewSource.在不完全打破 MVMM 模式的情况下,它充当了 ViewModel 内部 UI 控件的一种代表.

To control the behaviors of the UI control in question (here a DataGrid) one might simply use a CollectionViewSource. It acts as a kind of representative for the UI control inside your ViewModel without completely breaking the MVMM pattern.

在 ViewModel 中声明一个 CollectionViewSource 和一个普通的 ObservableCollection 并将 CollectionViewSource 包裹在 ObservableCollection 周围代码>:

In the ViewModel declare both a CollectionViewSource and an ordinary ObservableCollection<T> and wrap the CollectionViewSource around the ObservableCollection:

// Gets or sets the CollectionViewSource
public CollectionViewSource ViewSource { get; set; }

// Gets or sets the ObservableCollection
public ObservableCollection<T> Collection { get; set; }

// Instantiates the objets.
public ViewModel () {

    this.Collection = new ObservableCollection<T>();
    this.ViewSource = new CollectionViewSource();
    ViewSource.Source = this.Collection;
}

然后在应用程序的视图部分中,您无需将CollectionControlItemsSource 绑定到CollectionViewSource<的View 属性./code> 而不是直接到 ObservableCollection:

Then in the View part of the application you have nothing else to do as to bind the ItemsSource of the CollectionControl to the View property of the CollectionViewSource instead of directly to the ObservableCollection:

<DataGrid ItemsSource="{Binding ViewSource.View}" />

从现在开始,您可以使用 ViewModel 中的 CollectionViewSource 对象直接操作 View 中的 UI 控件.

From this point on you can use the CollectionViewSource object in your ViewModel to directly manipulate the UI control in the View.

例如排序 - 正如我的主要问题 - 看起来像这样:

Sorting for example - as has been my primary problem - would look like this:

// Specify a sorting criteria for a particular column
ViewSource.SortDescriptions.Add(new SortDescription ("columnName", ListSortDirection.Ascending));

// Let the UI control refresh in order for changes to take place.
ViewSource.View.Refresh();

你看,非常非常简单和直观.希望这能帮助其他像它一样帮助我的人.

You see, very very simple and intuitive. Hope that this helps other people like it helped me.

这篇关于有界数据更改后重新排序 WPF DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆