如何使 Silverlight DataGridTemplateColumn 标题居中?
我想将标题置于 Silverlight DataGridTemplateColumn 的中心。下面的代码让我明白了大部分内容:
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));
标题确实居中,但如果展开列,标题不会拉伸。它只是保持原来的宽度,在其两侧留下白色间隙,看起来很糟糕。
将列标题居中以使其仍然占据整个宽度的正确方法是什么?
I want to center the header on a Silverlight DataGridTemplateColumn. The following code gets me most of the way there:
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 header is, indeed, centered, but if the column is expanded, the header doesn't stretch. It just remains it's original width, leaving white gaps on either side of it, which looks terrible.
What is the proper way to center the column header, such that it still occupies the full width?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
HorizontalContentAlignment
属性设置为Center
。看来这里的内容是指标题的内容,而不是数据网格中单元格的内容。
Set the
HorizontalContentAlignment
Property toCenter
.It seems that Content here refers to the content of the header, not the content of the cells in the datagrid.