Sort WPF datagrid with integer appended with string

前端 未结 1 1670
盖世英雄少女心
盖世英雄少女心 2021-01-15 01:35

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

DG1.DataContext = a; 

One of the columns is having values like following

<
相关标签:
1条回答
  • 2021-01-15 02:01

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

    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)));
        }
    }
    

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

    <DataGridColumn x:Name="xxxxx"
                    SortMemberPath="YourSortOrder" 
                    ...etc...
    
    0 讨论(0)
提交回复
热议问题