Skip to content

Commit e068319

Browse files
authored
Merge pull request #279 from cricketthomas/main
Add `SortMemberPath` to TableViewColumn for custom sorting #278
2 parents df30b69 + 1cd97eb commit e068319

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Columns/TableViewColumn.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,20 @@ private static void OnHeaderStyleChanged(DependencyObject d, DependencyPropertyC
455455
}
456456
}
457457

458+
/// <summary>
459+
/// Gets or sets the member path to use for sorting instead of the default binding path.
460+
/// </summary>
461+
public string? SortMemberPath
462+
{
463+
get => (string?)GetValue(SortMemberPathProperty);
464+
set => SetValue(SortMemberPathProperty, value);
465+
}
466+
467+
/// <summary>
468+
/// Identifies the SortMemberPath dependency property.
469+
/// </summary>
470+
public static readonly DependencyProperty SortMemberPathProperty = DependencyProperty.Register(nameof(SortMemberPath), typeof(string), typeof(TableViewColumn), new PropertyMetadata(null));
471+
458472
/// <summary>
459473
/// Identifies the HeaderStyle dependency property.
460474
/// </summary>

src/ItemsSource/ColumnSortDescription.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public ColumnSortDescription(TableViewColumn column,
2121

2222
public override object? GetPropertyValue(object? item)
2323
{
24+
// Use reflection-based property access when SortMemberPath is explicitly provided; otherwise, fall back to column cell content.
25+
if (!string.IsNullOrEmpty(PropertyName))
26+
{
27+
return base.GetPropertyValue(item);
28+
}
2429
return Column.GetCellContent(item);
2530
}
2631

src/TableViewColumnHeader.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ private void DoSort(SD? direction, bool singleSorting = true)
111111
{
112112
var boundColumn = Column as TableViewBoundColumn;
113113
Column.SortDirection = direction;
114+
115+
// Prefer explicit SortMemberPath if provided, otherwise use bound column's property path
116+
var sortPath = Column.SortMemberPath ?? boundColumn?.PropertyPath;
117+
114118
_tableView.SortDescriptions.Add(
115-
new ColumnSortDescription(Column!, boundColumn?.PropertyPath, direction.Value));
119+
new ColumnSortDescription(Column!, sortPath, direction.Value));
116120

117121
_tableView.EnsureAlternateRowColors();
118122
}

0 commit comments

Comments
 (0)