使用附加字符串的整数对 WPF 数据网格进行排序 [英] Sort WPF datagrid with integer appended with string

查看:32
本文介绍了使用附加字符串的整数对 WPF 数据网格进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wpf 数据网格.我将 ObservableCollection 分配给它.

I have a wpf datagrid. I assign ObservableCollection to it.

DG1.DataContext = a; 

其中一列具有如下值

1_A_B
12_A1_B
3_A2_B
10_A3_B
2_A4_B
15_A5_B

我想使用第一个整数值对数据网格进行排序,如下所示

i want to sort the datagrid using the first integer value like following

1_A_B
2_A4_B
3_A2_B
10_A3_B
12_A1_B
15_A5_B

如果我使用此列进行排序,它将采用如下所示的字符串升序(这不是我想要的)

If i sort using this column, it is taking as string ascending like following(which is not i want)

1_A_B
10_A3_B
12_A1_B
15_A5_B
2_A4_B
3_A2_B

我想使用上列中的第一个整数值进行排序

I want to sort using the first integer value in above column

推荐答案

1_A_B12_A1_B 等值将包含在绑定到该列的属性中.我们称之为PropertyA.

The values 1_A_B, 12_A1_B etc will be contained within the property bound to that column. Let's call this PropertyA.

可能实现此目的的最简单方法是在数据对象上拥有另一个属性,我们将其称为 YourSortOrder.当使用非空值调用 PropertyA 上的 setter 时,您可以使用简单的字符串操作和 int.Parse() 提取数值并将其分配给 YourSortOrder.

Possibly the easiest way to achieve this is to have another property on the data object, let's call that YourSortOrder. When the setter on PropertyA gets called with a non null value, you can use simple string manipulation together with an int.Parse() to extract the numeric value and assign it to YourSortOrder.

此代码不是生产质量,但说明了这一点:

This code isn't production quality but illustrates the point:

public string PropertyA
{
    get { ... }
    set
    {
        _propertyA = value;
        if (value != null)
            YourSortOrder = int.Parse(value.Substring(0, value.IndexOf("_", StringComparison.InvariantCultureIgnoreCase)));
    }
}

然后将 DataGridColumn 的 SortMemberPath 设置为属性 YourSortOrder:

then set the SortMemberPath of your DataGridColumn to the property YourSortOrder:

<DataGridColumn x:Name="xxxxx"
                SortMemberPath="YourSortOrder" 
                ...etc...

这篇关于使用附加字符串的整数对 WPF 数据网格进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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