DataGrid表格的表头换行和内容居中

本文详细介绍了如何在全局App.xaml文件中声明资源字典,引用TableStyle.xaml来定义表格模版样式,包括单元格文本居中跨行显示和标题居中跨行显示。同时,在前台页面.xaml文件中通过绑定数据源来实现自定义样式应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1在全局App.xaml文件中声明

在<ResourceDictionary></ResourceDictionary>标记中
添加 <ResourceDictionary Source="TableStyle.xaml"/>
2在TableStyle.xaml文件的< ResourceDictionary></ResourceDictionary>标记中加入
 1  <!--表格模版 -->
 2     <!--单元格 文本居中 跨行-->
 3     <Style TargetType ="TextBlock" x: Key="dgCell">
 4         <Setter Property ="TextAlignment" Value="Center"/>
 5      <Setter Property="TextWrapping" Value ="Wrap"/>
 6     </Style>
 7     <!--标题 居中跨行-->
 8     <Style TargetType ="DataGridColumnHeader" x: Key="dgHeader">
 9         <Setter Property ="HorizontalContentAlignment" Value="Center"/>
10         <Setter Property ="ContentTemplate">
11             <Setter.Value>
12                 <DataTemplate>
13                    <TextBlock Text ="{Binding}" TextWrapping="Wrap"   TextAlignment ="Center"   HorizontalAlignment="Center"/>
14                 </DataTemplate>
15             </Setter.Value>
16         </Setter>
17     </Style>

 

3在前台页面.xaml文件中绑定数据源
在<DataGridTextColumn/>标记中添加
即为
1 <DataGridTextColumn ElementStyle ="{StaticResource dgCell }" HeaderStyle ="{ StaticResource dgHeader }"/>

 

转载于:https://www.cnblogs.com/JoanLin-workNotes/p/3751884.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值