简体   繁体   English

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

[英]Custom Sort in WPF DataGrid using MVVM

I have a datagrid that is bound to a collection of my ViewModel. 我有一个数据网格绑定到我的ViewModel的集合。 One of its column has values that are very specific to business requirements. 其中一列的值具有非常特定于业务需求的值。 On this column it can contain an alphanumeric characters. 在此列上,它可以包含字母数字字符。

For example I can have column values A1,A20,AA,AA12,AAA. 例如,我可以具有列值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. 使用DataGrid进行默认排序,但只进行常规排序。

My question is how would you implement this through MVVM? 我的问题是你将如何通过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. 然而,这不是我想要的,我正在寻找关于如何处理这个问题的建议或解决方案。

I found this link Sorting on datagrid column with binded data and converter that 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. 我发现这个链接在datagrid列上使用绑定数据和转换器对DataGrid附加属性进行排序,但我想要做的是在每次用户单击此列时附加要更新的属性。 Is it possible to attach a property in a DataGrid Column? 是否可以在DataGrid列中附加属性?

Possible duplicate of : Sorting on datagrid column with binded data and converter but this is not using MVVM. 可能重复: 使用绑定数据和转换器对datagrid列进行排序,但这不是使用MVVM。

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'. 第一列显示信息绑定到属性“名称”。 BUT when you click the column header, it will sort on a property which is not displayed, 'DriveType'. 但是当您单击列标题时,它将对未显示的属性“DriveType”进行排序。 Strange example, but it works fine. 奇怪的例子,但它工作正常。

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, ie, the longest value of 'MyName' will be first with shorter values after that. 这将导致排序执行您正在寻找的内容,即“MyName”的最长值将首先使用较短的值。 You have to repopulate 'MySortString' property every time you change sort methods or reload your data source. 每次更改排序方法或重新加载数据源时,都必须重新填充“MySortString”属性。

This strategy is MVVM compliant because all you are doing in the VM is populating an additional property. 此策略符合MVVM,因为您在VM中所做的只是填充其他属性。 Plus you can unit test it immediatly with Nunit or whatever. 另外,您可以使用Nunit或其他任何方式对其进行单元测试。

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. 希望这可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM