Sorting and Column Widths

This guide covers sorting behavior and column width mechanics.

Sorting Basics

Sorting is requested through source:

using System.ComponentModel;

Source.SortBy(Source.Columns[0], ListSortDirection.Ascending);

UI headers call this when clicked (if sorting is enabled).

Sort Enablement

Sorting must be enabled at both levels:

  • grid level: TreeDataGrid.CanUserSortColumns
  • column level: ColumnOptions<TModel>.CanUserSortColumn

If either disables sorting, user header click does not sort that column.

Sort Comparison Selection

Order of precedence for comparison:

  1. explicit CompareAscending / CompareDescending in column options
  2. default comparer generated by column implementation

SortBy(...) returns false when sorting cannot be performed for the given column.

Sort Indicators

After successful sort:

  • source updates SortDirection on sorted column
  • other columns clear sort direction

SortDirection is visual indicator; changing it directly does not sort source.

Width Fundamentals

Each column has preferred Width (GridLength) and runtime ActualWidth.

Supported width modes:

  • Auto
  • Pixel
  • Star

Adjust at runtime:

Source.Columns.SetColumnWidth(0, new GridLength(240, GridUnitType.Pixel));
Source.Columns.SetColumnWidth(1, GridLength.Auto);

Min/Max Constraints

From ColumnOptions<TModel>:

  • MinWidth
  • MaxWidth

Example:

new TextColumn<Person, string>(
    "Name",
    x => x.FirstName,
    options: new TextColumnOptions<Person>
    {
        MinWidth = new GridLength(80),
        MaxWidth = new GridLength(260),
    })

User Resize Behavior

User drag-resize requires:

  • grid CanUserResizeColumns = true
  • column CanUserResizeColumn != false (null inherits grid setting)

Double-clicking header resizer resets column to Auto width.

Star Width Distribution

Star columns share remaining viewport width after fixed/auto columns are measured.

If star widths hit min/max constraints, remaining unconstrained stars are recalculated.

Troubleshooting

Use these checks when sorting behavior or width calculations look wrong.

Drag/drop row move stops working after sort

Expected: row drag/drop move is disabled on sorted sources.

Column appears clipped

Review MinWidth, MaxWidth, and template measure behavior.

API Coverage Checklist