我需要在DataGrid上禁用多列排序。这可能吗?
答案 0 :(得分:2)
通过订阅DataGrid_Sorting事件并设置args'处理财产为真:
private void ResultsDataGrid_Sorting(object sender, DataGridSortingEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{
e.Handled = true;
}
}
答案 1 :(得分: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;
}
}
}
XAML
<DataGrid ...>
<i:Interaction.Behaviors>
<Kadia:DataGridICollectionViewSortMerkerBehavior />
</i:Interaction.Behaviors>