以编程方式对wpf数据网格进行排序

有没有办法对WPF DataGrid programmaticaly进行排序(例如,如果我点击我的第一列)。

有没有办法模拟这个点击? 还是最好的方式?

这是我的代码:

Collection_Evenements = new ObservableCollection(); Collection_Evenements = myEvenement.GetEvenementsForCliCode(App.obj_myClient.m_strCode); Collection_Evenements.CollectionChanged += Collection_Evenements_CollectionChanged; myDataGridEvenements.ItemsSource = Collection_Evenements; System.Data.DataView dv = (System.Data.DataView)myDataGridEvenements.ItemsSource; dv.Sort = "strEvtType"; myDataGridEvenements.Focus(); myDataGridEvenements.SelectedIndex = 0; myDataGridEvenements.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 

我不知道为什么,但行“dv.Sort =”strEvtType“;” 导致一个奇怪的事情,我的窗口显示和程序不继续执行下一行,但我没有看到那种!

非常感谢,

最好的祝福,

Nixeus

voo的解决方案对我不起作用, ItemsSource为null,很可能是因为它没有直接设置,而是绑定。 我在StackOverflow上找到的所有其他解决方案都只处理模型的排序,但DataGrid标题没有反映到排序。

以下是基于不完整脚本的正确解决方案: http : //dotnetgui.blogspot.co.uk/2011/02/how-to-properly-sort-on-wpf-datagrid.html

 public static void SortDataGrid(DataGrid dataGrid, int columnIndex = 0, ListSortDirection sortDirection = ListSortDirection.Ascending) { var column = dataGrid.Columns[columnIndex]; // Clear current sort descriptions dataGrid.Items.SortDescriptions.Clear(); // Add the new sort description dataGrid.Items.SortDescriptions.Add(new SortDescription(column.SortMemberPath, sortDirection)); // Apply sort foreach (var col in dataGrid.Columns) { col.SortDirection = null; } column.SortDirection = sortDirection; // Refresh items to display sort dataGrid.Items.Refresh(); } 

如果您的代码,它可以像这样使用:

 SortDataGrid(myDataGridEvenements, 0, ListSortDirection.Ascending); 

或者使用默认参数值,只需:

 SortDataGrid(myDataGridEvenements); 

获取ItemsSource的DataView并使用其Sort属性指定要排序的列:

 (yourDataGrid.ItemsSource as DataView).Sort = "NAME_OF_COLUMN"; 

您可以使用ICollectionView对数据网格中的项进行筛选,排序和分组。

编辑:添加排序,没仔细阅读问题:)

  var view = CollectionViewSource.GetDefaultView(this.MyData); view.Filter = ViewFilter; view.SortDescriptions.Add(new SortDescription("MyPropertyToSort", ListSortDirection.Descending)); private bool ViewFilter(object obj) { var item = obj as MyObject; if (item == null) return false; //your filter logik goes here if(item.MyStringProp.StartsWith("Test")) return false; return true; } 

我的方法对我有用。 试试这个代码。 对不起俄语

 // Если таблица пустая, то привязываем ее к журналу if(dgEvents.ItemsSource == null) dgEvents.ItemsSource = events.Entries; // Обновляем записи CollectionViewSource.GetDefaultView(dgEvents.ItemsSource).Refresh(); // Очищаем описание сортировки dgEvents.Items.SortDescriptions.Clear(); // Созадем описание сортировки dgEvents.Items.SortDescriptions.Add(new SortDescription(dgEvents.Columns[0].SortMemberPath, ListSortDirection.Descending)); // Очищаем сортировку всех столбцов foreach (var col in dgEvents.Columns) { col.SortDirection = null; } // Задаем сортировку времени по убыванию (последняя запись вверху) dgEvents.Columns[0].SortDirection = ListSortDirection.Descending; // Обновляем записи dgEvents.Items.Refresh(); 

DataGrid的PerformSort方法是在列的标题单击上实际执行的方法。 但是这种方法是内部的。 因此,如果您真的想模拟点击 ,则需要使用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] }); }