Reputation: 1011
Change the column header in Runtime (Datagrid)
Is there a way to do this?
How Change the background color?
Upvotes: 0
Views: 1361
Reputation: 36775
If you have the header named in code, you can try to change the style of the header:
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn x:Name="m_yourColumn" Header="Header" />
</DataGrid.Columns>
</DataGrid>
Style newStyle = new System.Windows.Style() { TargetType=typeof(Control)};
newStyle.Setters.Add(new Setter(Control.BackgroundProperty,new SolidColorBrush(Colors.Red)));
m_yourColumn.HeaderStyle = yourNewStyle();
What I don't know, is which property to set. Maybe you have to change the HeaderTemplate before you can change the background. In my example I set Control.Background, but maybe this is not enough.
Upvotes: 1
Reputation: 184346
Try something like this:
var style = new Style();
style.Setters.Add(new Setter(Border.BackgroundProperty, Brushes.Red)); //Brush of your choice here
_myColumn.HeaderStyle = style; //Column referenced via x:Name
Upvotes: 1