C# wpf DataGrid-仅在命令上排序
我有自己的C# wpf DataGrid-仅在命令上排序,c#,wpfdatagrid,C#,Wpfdatagrid,我有自己的DataGrid组件(继承自DataGrid)。我希望这个组件像MS Access网格一样工作。我需要的是在调用方法MySort()(MyDataGrid.MySort) MySort方法使用DataGriditems集合,因此我将SortDescription添加到items中,并对视图进行排序。问题是,我不想在添加或编辑项目时重新排序此网格。排序只能由MySort方法调用 当项时,如何防止DataGrid排序。SortDescription有一些值?我需要一些属性,如do\u no
DataGrid
组件(继承自DataGrid
)。我希望这个组件像MS Access网格一样工作。我需要的是在调用方法MySort()
(MyDataGrid.MySort)
MySort
方法使用DataGrid
items集合,因此我将SortDescription
添加到items
中,并对视图进行排序。问题是,我不想在添加或编辑项目时重新排序此网格。排序只能由MySort
方法调用
当项时,如何防止DataGrid
排序。SortDescription
有一些值?我需要一些属性,如do\u not\u resort
如果您使用的是.Net framework 4或更高版本,则可以使用网格控件的“CanUserSortColumns”属性来防止自动排序
自定义网格的MySort方法大致如下所示
public void MySort(DataGridSortingEventArgs args)
{
//create a collection view for the datasource binded with grid
ICollectionView dataView = CollectionViewSource.GetDefaultView(this.ItemsSource);
//clear the existing sort order
dataView.SortDescriptions.Clear();
ListSortDirection sortDirection = args.Column.SortDirection ?? ListSortDirection.Ascending;
//create a new sort order for the sorting that is done lastly
dataView.SortDescriptions.Add(new SortDescription(args.Column.SortMemberPath, sortDirection));
//refresh the view which in turn refresh the grid
dataView.Refresh();
}
Kumar Veluswam谢谢你快速回答,但我认为这并不能解决我的问题。这就是我自己做的。我将使用示例向您展示关键问题:DataGrid显示值:1 2 3 5 DataGrid是可编辑的,因此我将值3更改为7…..然后DataGrid resorts(因为SortDescriptions具有columnname)。我不希望在添加或修改项目时自动使用它。只能通过MySort方法进行重排序。CollectionViewSource会自动排序。您是否尝试过绑定通用列表而不是CollectionViewSource,因为它可以更好地控制排序?你是按多列排序的吗?