我正在使用 WPF 的 RADGridView 来显示一些数据。它是从数据库中动态提取的,所以我不知道列名或每个单元格中包含的数据类型。我想让用户在双击列标题时对每列的数据进行排序。
由于某种原因,网格没有排序。这就是我到目前为止所拥有的。
private void SetEventHandlers()
{
if (_grid != null)
{
_grid.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);
}
}
private void OnCellDoubleClick(object sender, RoutedEventArgs e)
{
GridViewCellBase cell = e.OriginalSource as GridViewCellBase;
if (cell != null && cell is GridViewHeaderCell)
{
SetSorting(cell);
}
}
private void SetSorting(GridViewCellBase cell)
{
GridViewColumn column = cell.Column;
SortingState nextState = GetNextSortingState(column.SortingState);
_grid.SortDescriptors.Clear();
if (nextState == SortingState.None)
{
column.SortingState = SortingState.None;
}
else
{
_grid.SortDescriptors.Add(CreateColumnDescriptor(column, nextState));
column.SortingState = nextState;
}
}
编辑:
private ColumnSortDescriptor CreateColumnDescriptor(GridViewColumn column, SortingState sortingState)
{
ColumnSortDescriptor descriptor = new ColumnSortDescriptor();
descriptor.Column = column;
if (sortingState == SortingState.Ascending)
{
descriptor.SortDirection = ListSortDirection.Ascending;
}
else
{
descriptor.SortDirection = ListSortDirection.Descending;
}
return descriptor;
}