Sort a wpf datagrid programmatically

前端 未结 6 2412
攒了一身酷
攒了一身酷 2020-11-29 08:22

Is there a way to sort a WPF DataGrid programmaticaly ( for example, like if i clicked on my first column).

Is there a way to simuate this click ? Or a best way ?

6条回答
  •  忘掉有多难
    2020-11-29 08:43

    PerformSort method of the DataGrid is what actually executed on a column's header click. However this method is internal. So if you really want to simulate the click you need to use reflection:

    public static void SortColumn(DataGrid dataGrid, int columnIndex)
    {
        var performSortMethod = typeof(DataGrid)
                                .GetMethod("PerformSort",
                                           BindingFlags.Instance | BindingFlags.NonPublic);
    
        performSortMethod?.Invoke(dataGrid, new[] { dataGrid.Columns[columnIndex] });
    }
    

提交回复
热议问题