有没有办法以编程方式对 WPF DataGrid 进行排序(例如,如果我单击第一列)?
有没有办法模拟这个点击?
这是我的代码:
Collection_Evenements = new ObservableCollection<Evenement>();
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";
行导致了一件奇怪的事情,我的窗口出现了,程序没有继续执行下一行,尽管如此,我没有看到排序!
voo 的解决方案对我不起作用,
ItemsSource
为空,很可能是因为它不是直接设置的,而是绑定的。
我在 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";
PerformSort 方法是在单击列标题时实际执行的方法。然而这个方法是内部的。所以如果你真的想模拟点击你需要使用反射:
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] });
}
快速简单的方法:
dgv.Items.SortDescriptions.Clear();
dgv.Items.SortDescriptions.Add(new SortDescription("ID", ListSortDirection.Descending));
dgv.Items.Refresh();
这个方法对我有用:
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();
您可以使用 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;
}
要像单击第一列一样对 DataGrid 进行排序,您必须使用从 WPF 创建的 DataView。 在此示例中,每次自动生成列时,DataGrid 都会进行排序,但您可以选择其他事件,例如 DataGrid1_Loaded
using System.Windows.Data;
private void DataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
{
(((DataGrid)sender).ItemsSource as DataView).Sort = DataGrid1.Columns[0].Header.ToString();
}