Skip to content

Commit 599a7ce

Browse files
committed
Add SortMemberPath to TableViewColumn for custom sorting
1 parent 92b8930 commit 599a7ce

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/Columns/TableViewColumn.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,21 @@ 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+
/// Holds the name of the member to use for sorting, if not using the default.
461+
/// </summary>
462+
public string? SortMemberPath
463+
{
464+
get => (string?)GetValue(SortMemberPathProperty);
465+
set => SetValue(SortMemberPathProperty, value);
466+
}
467+
468+
/// <summary>
469+
/// Identifies the SortMemberPath dependency property.
470+
/// </summary>
471+
public static readonly DependencyProperty SortMemberPathProperty = DependencyProperty.Register(nameof(SortMemberPath), typeof(string), typeof(TableViewColumn), new PropertyMetadata(null));
472+
458473
/// <summary>
459474
/// Identifies the HeaderStyle dependency property.
460475
/// </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+
// If a property name SortMemberPath was provided
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)