6.合并样式
在之前的文章C1DataGrid for WPF多表头合并教程:合并表头内容中,我们讨论过C1DataGrid的合并问题。
本文就在此基础上,来介绍合并文字的居中。
对于ColumnHeader上的文字,默认是在左边,现在对于这些文字,如果想要居中,可以有两种方法。
第一种:LoadedCellPresenter
通过LoadedCellPresenter事件设置TextAlignment属性,让文字居中。
代码参考:
c1DataGrid1.LoadedCellPresenter += (s, e) => { if (e.Cell.Presenter.Content is DataGridColumnHeaderPresenter) { var cc = (e.Cell.Presenter.Content as ContentControl); var tb = cc.Content as TextBlock; tb.TextAlignment = TextAlignment.Center; tb.HorizontalAlignment = HorizontalAlignment.Center; } };
第二种:XAML
在XAML中,设置HorizontalContentAlignment属性为Center。
参考代码:
首先设置column header style。
<!-- Column header style --> <Style x:Key="C1ColumnHeader" TargetType="c1:DataGridColumnHeaderPresenter"> <Setter Property="HorizontalContentAlignment" Value="Center" /> </Style>
然后通过设置TargetType引用设置的Style。
代码:
<Style TargetType="c1:C1DataGrid"> <Setter Property="ColumnHeaderStyle" Value="{StaticResource C1ColumnHeader}"/> </Style>
本文结束。
C1DataGrid的在线英文产品文档地址:
http://helpcentral.componentone.com/nethelp/C1datagridWPF/
如果你对C1DataGrid感兴趣,请到我们的官网下载最新版本:/download/?pid=6
如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-138.html