我需要禁用DataGrid上的多列排序。這可能嗎?如何在wpf DataGrid上禁用多列排序?
1
A
回答
1
我通過訂閱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;
}
}
+0
不錯的。我會用一種行爲來做 –
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>
相關問題
- 1. WPF Datagrid:清除列排序
- 2. WPF DataGrid:禁用多列選擇
- 3. WPF DataGrid不排序
- 4. ASP.NET DataGrid,可以在特定列上禁用排序嗎?
- 5. Datagrid中多列排序
- 6. WPF DataGrid - 在編輯行時禁用排序和篩選?
- 7. 禁用內置降序排序的wpf DataGrid控件
- 8. wpf datagrid:禁用datagridtemplatecolumn
- 9. DataGrid禁用行wpf
- 10. WPF Datagrid無法排序整數列
- 11. WPF中的Datagrid - 1列默認排序
- 12. WPF保持列排序與Datagrid
- 13. WPF Datagrid在具有空元素的列上排序
- 14. WPF Datagrid在列上排序綁定到包含數字的XML
- 15. WPF DataGrid:重新排列行?
- 16. WPF DataGrid組風格排序
- 17. WPF datagrid排序方向
- 18. wpf DataGrid - 僅命令排序
- 19. WPF工具包Datagrid排序
- 20. 排序WPF DataGrid,MVVM風格
- 21. WPF Datagrid組和排序
- 22. WPF禁用帶樣式的Datagrid列
- 23. WPF4 Datagrid不在列標題上排序
- 24. 如何使WPF Toolkit的DataGrid的模板列可排序?
- 25. 禁用Datagrid排序爲excel導出VB.net
- 26. 如何對WPF Toolkit DataGrid上的DataGridTemplateColumn進行排序?
- 27. WPF Datagrid:取消排序事件(WPF Bug?)
- 28. 使用MultiValueConverter排序DataGrid列
- 29. WPF禁用DataGrid單元格
- 30. WPF XAML:如何禁用DataGrid中的多選?
你能向我們展示一些關於目前爲止的代碼? – TheGeekZn
@NewAmbition:當然,我有一個DataGrid和一個自定義的分類器,但我不確定這是否會對您有所幫助...... –
好吧,在您正在編寫的代碼中提供答案會更容易如果我們知道你的代碼是什麼的話。 – TheGeekZn