如何在数据网格中将单元格内容居中?

32

我这样设置数据网格的最小高度:

<DataGrid MinRowHeight="40">
在向数据网格提供数据后,每个单元格中的文本都是顶部和左对齐的。我找不到一种简单的方法来使该文本居中。
你有什么建议吗?

1
这可能会有所帮助:http://www.kudinov.ru/?p=133 - Alexey
10个回答

51

最终解决方案:

<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>

3
如果你的网格使用除DataGridTextColumn以外的列,则这是正确的解决方案。如果你在网格上使用了模板列,那么使用 TextBlock 样式解决方案会在文本下方添加一条线。 如果有人能够解释一下这段代码正在做什么... - GBU

29

以下代码将使 DataGrid 中的单元格内容居中:

<Style TargetType="DataGridCell">
    <Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>

当使用带有文本列的内置数据网格时,这非常有效。 - galford13x
请注意,由于您实际上没有设置任何“DataGridCell”特定属性,因此可以省略“TargetType =” DataGridCell“”属性,并将其声明为应用程序.xaml中的通用样式。 - BrainSlugs83

9

您可以使用样式。我为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>

1
终于有一个可以设置为单列的解决方案了。感谢您! - C4d

9

使用ElementStyle

 <DataGridTextColumn ElementStyle="{StaticResource Centering}"/>

  <Style x:Key="Centering" TargetType="{x:Type TextBlock}">
       <Setter Property="HorizontalAlignment" Value="Center" />
  </Style>

6
尝试将DataGrid的Vertical和HorizontalContentAlignment设置为Center。
<DataGrid VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />

如果那个方法不起作用,你可以使用这个答案中的解决方案。它使用一种样式来对齐DataGrid单元格内容。

小心不要使用'VerticalAlignment',因为它只会将数据表格本身居中,而不会将其单元格(子元素)居中。 - undefined

5

这个样式会将所有的DataGridCellVerticalAlignment设置为Center,无需显式应用。

<Style TargetType="DataGridCell">
    <Style.Resources>
        <Style TargetType="ContentPresenter">
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </Style.Resources>
</Style>

我觉得这是最优雅的解决方案。

2

将您的样式标签修改为以下内容...

 <Style x:Key="CellTextCentre" TargetType="DataGridCell">
    <Setter Property="TextBlock.TextAlignment" Value="Center"></Setter>
    <Setter Property="TextBlock.VerticalAlignment" Value="Center"></Setter> 
 </Style>

2
这段代码可以将数据表格单元格的内容显示在中间:

以下代码可将数据表格单元格的内容显示在中间。

<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>

0

这里有一个更好的方法来垂直居中内容:

<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>

0

这里是Arpit Shah的回答的稍长版本。

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接