【发布时间】:2011-08-29 15:34:42
【问题描述】:
我这样设置数据网格的最小高度:
<DataGrid MinRowHeight="40">
在向数据网格输入数据后,每个单元格中的文本都是顶部和左侧对齐的。 我找不到使文本居中的简单方法。
有什么建议吗?
【问题讨论】:
-
这可能会有所帮助:kudinov.ru/?p=133
标签: wpf datagrid row alignment
我这样设置数据网格的最小高度:
<DataGrid MinRowHeight="40">
在向数据网格输入数据后,每个单元格中的文本都是顶部和左侧对齐的。 我找不到使文本居中的简单方法。
有什么建议吗?
【问题讨论】:
标签: wpf datagrid row alignment
最终解决方案:
<Style x:Key="DataGridContentCellCentering" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【讨论】:
以下代码将 DataGrid 中单元格的内容居中:
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
【讨论】:
DataGridCell 特定属性,因此您可以省略 TargetType="DataGridCell" 属性,并将其声明为 app.xaml 中的通用样式。
使用元素样式
<DataGridTextColumn ElementStyle="{StaticResource Centering}"/>
<Style x:Key="Centering" TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
【讨论】:
您可以使用样式。我为 DataGridCheckBoxColumn 添加了一个示例,希望它能让您朝着正确的方向前进。
<DataGridCheckBoxColumn Header="is active" IsReadOnly="False">
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataGridCheckBoxColumn.ElementStyle>
<DataGridCheckBoxColumn.Binding>
<Binding Path="ISACTIVE" ValidatesOnDataErrors="True" Converter="{StaticResource MyBoolToIsActiveConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"
NotifyOnValidationError="True" ValidatesOnExceptions="True">
</Binding>
</DataGridCheckBoxColumn.Binding>
</DataGridCheckBoxColumn>
【讨论】:
尝试将 DataGrid 的 Vertical 和 HorizontalContentAlignment 设置为 Center
<DataGrid VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
如果这不起作用,您可以使用this answer 中的解决方案。它使用对齐 DataGrid 单元格内容的样式
【讨论】:
像这样修改你的样式标签.....
<Style x:Key="CellTextCentre" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"></Setter>
<Setter Property="TextBlock.VerticalAlignment" Value="Center"></Setter>
</Style>
【讨论】:
此样式会将所有DataGridCells 的VerticalAlignment 设置为Center,而无需显式应用。
<Style TargetType="DataGridCell">
<Style.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</Style.Resources>
</Style>
我发现这是最优雅的解决方案。
【讨论】:
以下代码在中心显示数据网格单元格内容..
<Style x:Key="DataGridCellStyle" TargetType="DataGridCell">
<!--DISPLAY CONTENT IN MIDDLE-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【讨论】:
这是Arpit Shah's answer 的稍长版本。
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Rows}">
<DataGrid.Resources>
<Style x:Key="RightAligned" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn CellStyle="{StaticResource RightAligned}"
Binding="{Binding Path=Quantity, Mode=OneWay}"
Header="Quantity" Width="60" />
<DataGridTextColumn Binding="{Binding Path=Category, Mode=OneWay}"
Header="Category" Width="150" />
</DataGrid.Columns>
</DataGrid>
【讨论】:
这里是垂直居中内容的更好方法:
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【讨论】: