使用 MVVM 在 WPF DataGrid 中自定义排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17206421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:02:35  来源:igfitidea点击:

Custom Sort in WPF DataGrid using MVVM

c#wpfsortingmvvmdatagrid

提问by Jepoy_D_Learner

I have a datagrid that is bound to a collection of my ViewModel. One of its column has values that are very specific to business requirements. On this column it can contain an alphanumeric characters.

我有一个绑定到我的 ViewModel 集合的数据网格。其中一列具有非常特定于业务需求的值。在此列上,它可以包含字母数字字符。

For example I can have column values A1,A20,AA,AA12,AAA. Now I want to custom sort this values, say I want the anything with most letters should go first or etchetera. There is a default sorting with DataGrid but only do normal sorting.

例如,我可以有列值 A1、A20、AA、AA12、AAA。现在我想对这些值进行自定义排序,比如说我想要字母最多的任何东西都应该排在第一位或 etchetera。DataGrid 有一个默认排序,但只进行正常排序。

My question is how would you implement this through MVVM? We can get away with this through subscribing to an event in code behind and re arrange the whole collection. However this is not what I want, I am looking for suggestions or solutions on how to approach this.

我的问题是您将如何通过 MVVM 实现这一点?我们可以通过在后面的代码中订阅一个事件并重新安排整个集合来解决这个问题。但是,这不是我想要的,我正在寻找有关如何处理此问题的建议或解决方案。

I found this link Sorting on datagrid column with binded data and converterthat is attaching a property for a DataGrid but what I want to do is to attach a property to be updated every time a user click on this column. Is it possible to attach a property in a DataGrid Column?

我发现这个链接Sorting on datagrid column with bounded data and converterthat is attaching a property for a DataGrid 但是我想要做的是附加一个属性,每次用户点击这个列时都会更新。是否可以在 DataGrid 列中附加属性?

Possible duplicate of : Sorting on datagrid column with binded data and converterbut this is not using MVVM.

可能的重复项:对具有绑定数据和转换器的数据网格列进行排序,但这不是使用 MVVM。

回答by Gayot Fow

There are several strategies, but the most immediately accessible is to set up a DataGrid something like this...

有几种策略,但最直接的方法是设置一个像这样的 DataGrid ......

<DataGrid ItemsSource="{Binding DriveList}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" SortMemberPath="DriveType"/>
    </DataGrid.Columns>
</DataGrid>

This example shows the grid binding to a list of drives on the host machine. The first column shows the information is bound to the property 'Name'. BUTwhen you click the column header, it will sort on a property which is not displayed, 'DriveType'. Strange example, but it works fine.

此示例显示网格绑定到主机上的驱动器列表。第一列显示绑定到属性“名称”的信息。 但是,当您单击列标题时,它将根据未显示的属性“DriveType”进行排序。奇怪的例子,但它工作正常。

So in your app, you would amend your collection items to include a property that is not displayed, and populate it with values according to what you want. In the example in your question, you might use something like...

因此,在您的应用程序中,您将修改集合项以包含未显示的属性,并根据需要为其填充值。在您问题的示例中,您可能会使用类似...

MySortString = MyName.ToString().Length;

And that will cause the sort to do what you are looking for, i.e., the longest value of 'MyName' will be first with shorter values after that. You have to repopulate 'MySortString' property every time you change sort methods or reload your data source.

这将导致排序执行您要查找的内容,即,'MyName' 的最长值将首先出现,然后是较短的值。每次更改排序方法或重新加载数据源时,都必须重新填充“MySortString”属性。

This strategy is MVVM compliant because all you are doing in the VM is populating an additional property. Plus you can unit test it immediatly with Nunit or whatever.

此策略符合 MVVM,因为您在 VM 中所做的一切都是填充附加属性。另外,您可以立即使用 Nunit 或其他工具对其进行单元测试。

回答by trilson86

I created an attached behavior which does this in a nice MVVM-friendly way:

我创建了一个附加的行为,它以一种对 MVVM 友好的方式执行此操作:

WPF DataGrid CustomSort for each Column

每列的 WPF DataGrid CustomSort

Hopefully this addresses your problem.

希望这可以解决您的问题。