【问题标题】:Update the WPF DataGrid Column Header Text via code behind通过后面的代码更新 WPF DataGrid 列标题文本
【发布时间】:2012-11-02 09:48:04
【问题描述】:

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

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

【问题讨论】:

  • 在这里工作正常。您的 DataGridColumn 是否使用标题模板?
  • 是的。DataGrid 是自定义的。

标签: wpf datagrid


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    • 2011-12-16
    相关资源
    最近更新 更多