Back to Devexpress

TreeViewControl.CustomNodeSort Event

wpf-devexpress-dot-xpf-dot-grid-dot-treeviewcontrol-3600ac8c.md

latest3.6 KB
Original Source

TreeViewControl.CustomNodeSort Event

Allows you to use custom rules to sort data.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public event TreeViewCustomNodeSortEventHandler CustomNodeSort
vb
Public Event CustomNodeSort As TreeViewCustomNodeSortEventHandler

Event Data

The CustomNodeSort event's data class is DevExpress.Xpf.Grid.TreeList.TreeViewCustomNodeSortEventArgs.

Remarks

Follow the steps below to apply custom logic to sort data:

  1. Set the SortOrder property to Ascending or Descending.
  2. Set the SortMode property to Custom.
  3. Handle the CustomNodeSort event.

When you handle this event, you need to compare two nodes. The Value1 and Value2 parameters identify the values of these nodes. Set the result of the comparison to the Result parameter as follows:

  • -1 to position the first row above the second row when data is sorted in ascending order. When data is sorted in descending order, the TreeViewControl positions the first row below the second row.
  • 1 to position the first row below the second row when data is sorted in ascending order. When data is sorted in descending order, the TreeViewControl positions the first row above the second row.
  • 0 to indicate that the rows are equal. In this case, the TreeViewControl arranges rows according to their indices in a data source.

Set the Handled parameter to true to use the custom comparison. Leave this parameter set to false to invoke the default comparison mechanism after your event handler has finished. In this instance, the TreeViewControl ignores the custom comparison’s result.

Example

xaml
<dxg:TreeViewControl SortOrder="Ascending"
                     SortMode="Custom" 
                     CustomNodeSort="treeview_CustomNodeSort"
                     ...>
csharp
void treeview_CustomNodeSort(object sender, DevExpress.Xpf.Grid.TreeList.TreeViewCustomNodeSortEventArgs e) {
    int dayIndex1 = GetDayIndex((string)e.Value1);
    int dayIndex2 = GetDayIndex((string)e.Value2);
    e.Result = dayIndex1.CompareTo(dayIndex2);
    e.Handled = true;
}
int GetDayIndex(string day) {
    return (int)Enum.Parse(typeof(DayOfWeek), day);
}
vb
Private Sub treeview_CustomNodeSort(ByVal sender As Object, ByVal e As DevExpress.Xpf.Grid.TreeList.TreeViewCustomNodeSortEventArgs)
    Dim dayIndex1 As Integer = GetDayIndex(CStr(e.Value1))
    Dim dayIndex2 As Integer = GetDayIndex(CStr(e.Value2))
    e.Result = dayIndex1.CompareTo(dayIndex2)
    e.Handled = True
End Sub

Private Function GetDayIndex(ByVal day As String) As Integer
    Return CInt([Enum].Parse(GetType(DayOfWeek), day))
End Function

See Also

TreeViewControl Class

TreeViewControl Members

DevExpress.Xpf.Grid Namespace