通过隐藏的代码更新 WPF DataGrid 列标题文本

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

Update the WPF DataGrid Column Header Text via code behind

wpfdatagrid

提问by Jean Paul Scott

How can i change the WPF DataGrid Column Header Text via code behind? i tried the code below but its not working.

如何通过隐藏的代码更改 WPF DataGrid 列标题文本?我尝试了下面的代码,但它不起作用。

this.sampleDataGrid.Columns[0].Header = "New Header"; 
this.sampleDataGrid.Refresh();

回答by Bizhan

If your DataGridColumn is a Template then you need to change its Template in code.

如果您的 DataGridColumn 是一个模板,那么您需要在代码中更改其模板。

    var template = new DataTemplate();
    template.VisualTree = new FrameworkElementFactory(typeof(TextBlock));
    template.VisualTree.SetValue(TextBlock.TextProperty, "New Header");
    dataGrid.Columns[0].HeaderTemplate = template;