通过代码更新WPF DataGrid列头文本

3
我该如何通过后台代码更改WPF DataGrid列标题文本? 我尝试了下面的代码,但它没有起作用。
this.sampleDataGrid.Columns[0].Header = "New Header"; 
this.sampleDataGrid.Refresh();

这里运行良好。你的DataGridColumn是否使用了标题模板? - Bizhan
是的。DataGrid是一个定制化的控件。 - Jean Paul
1个回答

3
如果您的DataGridColumn是一个模板,则需要在代码中更改其模板。
    var template = new DataTemplate();
    template.VisualTree = new FrameworkElementFactory(typeof(TextBlock));
    template.VisualTree.SetValue(TextBlock.TextProperty, "New Header");
    dataGrid.Columns[0].HeaderTemplate = template;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接