3

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

this.sampleDataGrid.Columns[0].Header = "New Header"; 
this.sampleDataGrid.Refresh();
Clemens
  • 123,504
  • 12
  • 155
  • 268
Jean Paul
  • 2,389
  • 5
  • 27
  • 37

1 Answers1

3

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

    var template = new DataTemplate();
    template.VisualTree = new FrameworkElementFactory(typeof(TextBlock));
    template.VisualTree.SetValue(TextBlock.TextProperty, "New Header");
    dataGrid.Columns[0].HeaderTemplate = template;
Bizhan
  • 16,157
  • 9
  • 63
  • 101