Hus*_*lil 12 c# wpf wpfdatagrid
我在我的应用程序中使用WPF Datagrid,可以通过单击标题对列进行排序.
我想知道是否有任何方法可以以编程方式清除列的排序?
我尝试对列进行排序然后清除MyDataGrid.Items.SortDescriptions,但该集合为空(即使已对一列进行了排序).
我也尝试过:
MyDataGridColumn.SortDirection = null;
Run Code Online (Sandbox Code Playgroud)
问题是列指示消失了,但是在编辑单元格和切换行时仍然会发生排序.
有没有办法清除列的排序?
编辑(为清晰起见):问题是,如果用户重新点击相同的列标题,我想再次允许排序,因此将CanUserSort设置为false会有问题,即使它是在XAML中完成的.简而言之,我正在尝试做的是,一旦已排序的列具有已修改的单元格,就会阻止对行进行排序.我想强制用户重新点击标题.
NoO*_*One 14
这是你需要的:
using System.Windows.Data;
using System.ComponentModel;
ICollectionView view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
if (view != null)
{
view.SortDescriptions.Clear();
foreach (DataGridColumn column in grid.Columns)
{
column.SortDirection = null;
}
}
Run Code Online (Sandbox Code Playgroud)
原始来源:https://stackoverflow.com/a/9533076/964053
我想知道的是M $想要不放置ClearSort()方法...
作为扩展...
public static void ClearSort(this DataGrid grid)
{
var view = CollectionViewSource.GetDefaultView(grid.ItemsSource);
view?.SortDescriptions.Clear();
foreach (var column in grid.Columns)
{
column.SortDirection = null;
}
}
Run Code Online (Sandbox Code Playgroud)
将所有列设置CanUserSort为-false
foreach (var a in MyDataGrid.Columns)
{
a.CanUserSort = false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10174 次 |
| 最近记录: |