实现自定义列排序

时间:2016-05-20 19:02:25

标签: wpf vb.net xaml sorting datagrid

我在datagrid列上实现自定义排序时遇到问题。有人可以解释我做错了吗?

我正在尝试为列创建自定义排序,以便首先按长度排序字符串,然后按值排序。它到达我的自定义函数,点击“抛出未实现的行”,然后给我一个InvalidOperationException错误。

Private Sub customSorting(sender As Object, e As DataGridSortingEventArgs) Handles grid.Sorting

    If e.Column.Header = "Test" Then
        Dim comp As IComparer = New RowComparer(e.Column.SortDirection)
        Dim lcv As ListCollectionView = CollectionViewSource.GetDefaultView(grid.ItemsSource)
        lcv.CustomSort = comp
        e.Handled = True
    End If
End Sub

Private Class RowComparer
    Implements System.Collections.IComparer
    Private o As Integer = 0 

    Public Sub New(order As Integer)
        o = order
    End Sub

    Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare
        Throw New NotImplementedException()
        If x.ToString.Length > y.ToString.Length Then
            If o = 0 Then
                Return 1
            Else
                Return -1
            End If
        ElseIf x.ToString.Length < y.ToString.Length Then
            If o = 0 Then
                Return -1
            Else
                Return 1
            End If
        Else
            Return String.Compare(x, y)
        End If
    End Function
End Class

1 个答案:

答案 0 :(得分:0)

Visual Studio自动生成了'Throw New NotImplementedException()',它正在停止我的程序。