How can I center the Silverlight DataGridTemplateColumn header?

I want to center a title on a Silverlight DataGridTemplateColumn. The following code will get me most of the way:

DataGridTemplateColumn column = new DataGridTemplateColumn();
column.CellTemplate = Resources[templateName] as DataTemplate;
column.Header = headerName;
column.HeaderStyle = new Style { TargetType = typeof(DataGridColumnHeader) };
column.HeaderStyle.Setters.Add(new Setter(DataGridColumnHeader.HorizontalAlignmentProperty, HorizontalAlignment.Center));

      

The heading is indeed centered, but if the column is expanded the heading is not stretched. It just stays the original width, leaving white gaps on either side of it, which looks terrible.

What is the correct way to center the column header so that it still takes up the full width?

+2


a source to share


1 answer


Set the property to a HorizontalContentAlignment

value Center

.



It seems that Content here refers to the content of the header, not the content of the cells in the datagrid.

+2


a source







All Articles