重新梳理WPF的DataGrid界后数据已更改 [英] Re-sort WPF DataGrid after bounded Data has changed

查看:634
本文介绍了重新梳理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)

任何想法?

推荐答案

我花了整个下午,但我终于< STRONG>找到了解决办法这是令人惊讶的简单的和的有效的:

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

要控制有问题的UI控件的行为(这里的DataGrid )人们可以简单地使用 Col​​lectionViewSource 。它作为一种代表您的视图模型里面的UI控件没有彻底打破了MVMM格局。

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.

在视图模型声明既是 Col​​lectionViewSource 和一个普通的的ObservableCollection< T> ,敷 Col​​lectionViewSource 围绕的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;
}



然后在应用程序的视图部分,你有没有别的事情可做,以绑定 Col​​lectionControl 来的视图属性 Col​​lectionViewSource 的ItemsSource C>,而不是直接将的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}" />



从这个角度上,你可以使用 Col​​lectionViewSource 对象在你的视图模型直接操作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天全站免登陆