简体   繁体   中英

Sorting Columns of type decimal? (nullable) in WPF DataGrid

I am working with a DataGrid in WPF with several decimal columns. I recently changed the bound properties to decimal? s and now these columns are not able to be sorted by clicking on the header (as my other columns are). However, I can still sort them using methods in my code-behind. Does anybody know if it's possible to sort the decimal? columns by clicking the header or what the reason behind it is if it's not possible? Here is my relevant code:

A couple of the columns in question (The attribute attaches to a behavior for the DataGrid. Enabling/disabling the behavior makes no difference):

[Column("PPAvg", 7)]
public decimal? ProjectedPointsAvg { get; set; }

[Column("PPHi", 8)]
public decimal? ProjectedPointsHi { get; set; }

[Column("PPLo", 9)]
public decimal? ProjectedPointsLo { get; set; }

The DataGrid itself:

<DataGrid x:Name="poolDataGrid"
          Grid.Row="1"
          CanUserAddRows="False"
          IsReadOnly="True"
          MouseDoubleClick="poolDataGrid_MouseDoubleClick">
    <i:Interaction.Behaviors>
        <local:ColumnHeaderBehavior />
    </i:Interaction.Behaviors>
</DataGrid>

I can still sort via code-behind using something like this:

var col = poolDataGrid.Columns.SingleOrDefault(c => c is DataGridTextColumn && c.Header.ToString() == "PPHi");
poolDataGrid.Items.SortDescriptions.Add(new SortDescription(col.SortMemberPath, ListSortDirection.Descending));

Unfortunately, I don't have much else to add. If anybody could help me out with this I would greatly appreciate it.

DataGridColumn.CreateDefaultColumn(ItemPropertyInfo) tests PropertyType for IComparable and sets CanUserSort to false for nullable types.

To enable standard sorting on autogenerated columns for nullable primitives:

void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (!e.Column.CanUserSort)
    {
        Type type = e.PropertyType;
        if (type.IsGenericType && type.IsValueType && typeof(IComparable).IsAssignableFrom(type.GetGenericArguments()[0]))
        {
            // allow nullable primitives to be sorted
            Debug.Assert(type.Name == "Nullable`1");
            e.Column.CanUserSort = true;
        }
    }
}

This works (at least) for nullable primitive types.

You can sort a DataGrid with nullable property but you need to define all columns on your own. So it should be something like this:

<DataGrid Name="poolDataGrid" AutoGenerateColumns="False" >
   <DataGrid.Columns>
      <DataGridTextColumn Header="Column1"  Binding="{Binding ProjectedPointsAvg }"/>
      <DataGridTextColumn Header="Column2" Binding="{Binding ProjectedPointsHi }" />
      <DataGridTextColumn Header="Column3" Binding="{Binding ProjectedPointsLo}" />
   </DataGrid.Columns>
</DataGrid>

Why does AutoGenerateColumns have problems with nullalble properties?

It appears that columns are only auto-generated for properties whose type is a bindable type.

More: http://vaultofthoughts.net/IsBindableTypeMysteryMethod.aspx

Tested on: VS2012 .NET 4.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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