如何垂直居中行? WPF DataGrid 中的内容?
我想在DataGrid中使数据记录的VerticalAlignment=Center
。默认情况下,数据记录的VerticalAlignment=Top
,看起来很难看。可以给我这个款式吗?
我在 App.xaml 文件中定义我的样式。以下是我当前的DataGrid样式:
<Style x:Key="DataGridView" TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="AliceBlue"></Setter>
<Setter Property="AlternationCount" Value="1"></Setter>
<Setter Property="AutoGenerateColumns" Value="False"></Setter>
<Setter Property="GridLinesVisibility" Value="Horizontal"></Setter>
<Setter Property="VerticalGridLinesBrush" Value="DarkGray"></Setter>
<Setter Property="HorizontalGridLinesBrush" Value="DarkGray"></Setter>
<Setter Property="RowHeight" Value="32"></Setter>
</Style>
I want to make the data record's VerticalAlignment=Center
in DataGrid. By default,the data record's VerticalAlignment=Top
, It looks uglily. Can you provide me this style?
I define my style in App.xaml file. The following is my current DataGrid style:
<Style x:Key="DataGridView" TargetType="DataGrid">
<Setter Property="AlternatingRowBackground" Value="AliceBlue"></Setter>
<Setter Property="AlternationCount" Value="1"></Setter>
<Setter Property="AutoGenerateColumns" Value="False"></Setter>
<Setter Property="GridLinesVisibility" Value="Horizontal"></Setter>
<Setter Property="VerticalGridLinesBrush" Value="DarkGray"></Setter>
<Setter Property="HorizontalGridLinesBrush" Value="DarkGray"></Setter>
<Setter Property="RowHeight" Value="32"></Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
DataGrid
上设置VerticalContentAlignment="Center"
:或者您可以向样式添加 setter:
当应用于
ItemsControl
时,此属性通常会修改每个单独项目容器的对齐方式。在您的情况下,这应该使所有行将其内容对齐到中心。更新
似乎 WPF 内置
DataGrid
不遵循规则。该解决方案取决于您使用的色谱柱的类型。
对于
DataGridTemplateColumn
使用CellTemplate
,如下所示:对于
DataGridTextColumn
,设置ElementStyle
:我已在
上尝试过此操作>DataGridTextColumn
,但该属性来自其父级DataGridBoundColumn
,因此也适用于DataGridCheckBoxColumn
和DataGridHyperlinkColumn
。更新 2
顺便说一句,还有另一种解决方案。
Try setting
VerticalContentAlignment="Center"
on theDataGrid
:or you can add a setter to your style:
When applied to
ItemsControl
s, this property usually modifies alignment of each individual item container. In your case, this should make all rows align their contents to center.UPDATE
Seems like the WPF built-in
DataGrid
doesn't follow the rule.The solution depends on the type of the columns you use.
For
DataGridTemplateColumn
use aCellTemplate
like this:For
DataGridTextColumn
, setElementStyle
:I've tried this on
DataGridTextColumn
, but the property is from its parentDataGridBoundColumn
, so should work forDataGridCheckBoxColumn
andDataGridHyperlinkColumn
also.Update 2
BTW, there's another solution.