禁用对 WPF MVVM 中 DataGrid 中自动生成的列进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25662442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:27:49  来源:igfitidea点击:

Disable sorting on autogenerated columns in DataGrid in WPF MVVM

wpfsortingmvvmdatagrid

提问by AxdorphCoder

I have a DataGrid in WPF with autogeneratedcolumns.

我在 WPF 中有一个带有自动生成列的 DataGrid 。

How can I disablesorting functionality of all the rows following the MVVMpattern?

如何禁用遵循MVVM模式的所有行的排序功能?

<DataGrid AutoGenerateColumns="True" 
            ItemsSource="{Binding MyList}" 
</DataGrid>

回答by Rohit Vats

Set CanUserSortColumns="False"on dataGrid which will disable sorting for all columns.

CanUserSortColumns="False"在 dataGrid 上设置这将禁用所有列的排序。

<DataGrid AutoGenerateColumns="True" 
          ItemsSource="{Binding MyList}"
          CanUserSortColumns="False">
</DataGrid>