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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6072093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:52:00  来源:igfitidea点击:

How to center the content of cells in a data grid?

wpfdatagridrowalignment

提问by Francois

I set the min height of a datagrid that way:

我以这种方式设置了数据网格的最小高度:

<DataGrid MinRowHeight="40">

After feeding the datagrid with datas, the text in each cell is top and left aligned. I could not find an easy way to center that text.

向数据网格提供数据后,每个单元格中的文本将顶部和左侧对齐。我找不到一种简单的方法来使该文本居中。

Any suggestions for doing that?

这样做有什么建议吗?

回答by Francois

Final solution:

最终解决方案:

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

回答by lea

The following code will center the contents of cells in DataGrid:

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

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

回答by blindmeis

you can use styles. i add a sample for DataGridCheckBoxColumn, i hope it put you in the right direction.

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

回答by Meysam Chegini

use ElementStyle

使用元素样式

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

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

回答by Rachel

Try setting the DataGrid's Vertical and HorizontalContentAlignment to Center

尝试将 DataGrid 的 Vertical 和 Horizo​​ntalContentAlignment 设置为 Center

<DataGrid VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />

If that doesn't work, you can use the solution in this answer. It uses a style that aligns the DataGrid cell contents

如果这不起作用,您可以使用此答案中的解决方案。它使用对齐 DataGrid 单元格内容的样式

回答by Arpit Shah

Modify your style tag like this.....

像这样修改你的样式标签......

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

回答by The Pademelon

This style will set the VerticalAlignmentto Centerfor all DataGridCells without needing to be applied explicitly.

这种样式设置VerticalAlignmentCenter所有DataGridCell•不用需要被明确地应用。

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

I find this to be the most elegant solution.

我发现这是最优雅的解决方案。

回答by Apfelkuacha

Here a better approach to center the content vertically:

这是垂直居中内容的更好方法:

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

回答by Steven Davis

Here is a slightly longer version of Arpit Shah's answer.

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