我需要在 DataGrid 上禁用多列排序。这可能吗?
问问题
1415 次
2 回答
2
通过订阅 DataGrid_Sorting 事件并将 args 的 Handled 属性设置为 true,我取得了成功:
private void ResultsDataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
e.Handled = true;
}
}
于 2014-03-13T17:44:54.617 回答
0
你可以创建一个行为并处理你自己的排序。以下未经测试:)
public class DataGridICollectionViewSortMerkerBehavior : Behavior<DataGrid>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Sorting += AssociatedObjectSorting;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Sorting -= AssociatedObjectSorting;
}
void AssociatedObjectSorting(object sender, DataGridSortingEventArgs e)
{
var view = AssociatedObject.ItemsSource as ICollectionView;
var propertyname = e.Column.SortMemberPath;
e.Column.SortDirection = this.GetSortArrowForColumn(e.Column);
if (view == null)
return;
view.SortDescriptions.Clear();
var sort = new SortDescription(propertyname, (ListSortDirection)e.Column.SortDirection);
view.SortDescriptions.Add(sort);
e.Handled = true;
}
private ListSortDirection GetSortArrowForColumn(DataGridColumn col)
{
if (col.SortDirection == null)
{
return ListSortDirection.Ascending;
}
else
{
return col.SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
}
}
}
xml
<DataGrid ...>
<i:Interaction.Behaviors>
<Kadia:DataGridICollectionViewSortMerkerBehavior />
</i:Interaction.Behaviors>
于 2013-07-19T13:22:24.283 回答