通过 WPF DataGrid 中的列标题获取列索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13071346/
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 05:56:57  来源:igfitidea点击:

Get column index by Column header in WPF DataGrid

c#wpfdatagrid

提问by Kishor

How to get the Columnindex or Column.DisplayIndexfrom its Column Headerin WPFDataGrid?

如何获取Column索引或Column.DisplayIndex从它的Column Headerin WPFDataGrid

I know the Column Header, want to get column index.

我知道了Column Header,想要得到column index

回答by paul

you could use DisplayIndex(be aware this will change if columns are resorted)

您可以使用DisplayIndex(请注意,如果重新使用列,这会改变)

var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;

edited: thanks for suggestion from @AnHX

编辑:感谢@AnHX 的建议

回答by AnHX

Look like "paul" have an small error. Here is my code:

看起来“保罗”有一个小错误。这是我的代码:

var index = dataGrid.Columns.Single(c => c.Header.ToString() == "HeaderName").DisplayIndex;