56

我正在用 WPF 编程。我需要一种在 DataGrid 控件中制作单元格中心内容的方法。我也使用此代码:

<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="10,10,0,0"
    VerticalAlignment="Top" Height="360" Width="498"
    FontFamily="2  Badr" FontSize="18" 
    AlternatingRowBackground="LightCoral" FlowDirection="RightToLeft"
    HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
    Background="{x:Null}"/>

怎么了?

4

9 回答 9

118

您需要设置 DataGridCell 样式

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
于 2013-08-12T03:15:00.257 回答
23

对于那些只需要在 VB.NET 中从自定义 XAML 样式格式化一个动态 DataGrid 列的人:

在 Application.xaml 中:

<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="DataGridCellCentered" TargetType="DataGridCell">
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

在 VB.NET 代码中:

Me.MyDataGrid.Columns(5).CellStyle = TryFindResource("DataGridCellCentered")

问候!

于 2016-03-14T19:56:45.867 回答
13

如其他答案所述:

<Setter Property="HorizontalAlignment" Value="Center" />

水平对齐

这将影响任何其他样式,例如背景。要仅使文本居中,请改用:

<Setter Property="TextAlignment" Value="Center" />

文本对齐

于 2016-01-23T17:36:52.823 回答
8

也许只是创造一种风格:

<Window.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
</Window.Resources>  

已编辑。

于 2013-08-12T09:32:43.380 回答
3

影响所有列

 <Window.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
 </Window.Resources>
于 2015-12-11T13:04:32.493 回答
1

如果您想在 DataGridTemplateColumn 中居中日期

 <DataGridTemplateColumn SortMemberPath="DataDiNascita" Header="Data di nascita" IsReadOnly="False">
                <DataGridTemplateColumn.CellEditingTemplate>                        
                    <DataTemplate>
                        <DatePicker SelectedDate="{Binding Path=DataDiNascita,Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Left">                                
                        </DatePicker>                           
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellTemplate>                        
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataDiNascita,Mode=TwoWay,StringFormat=\{0:dd/MM/yyyy\}}"  VerticalAlignment="Center" HorizontalAlignment="Left">                              
                        </TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
于 2014-03-27T08:00:56.923 回答
1

如何在 WPF DataGrid 中将文本居中

<DataGrid >
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
        </Style>
    </DataGrid.CellStyle>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Label.HorizontalContentAlignment" Value="Center" />
        </Style>
    </DataGrid.ColumnHeaderStyle>
</DataGrid>
于 2019-07-13T17:01:24.957 回答
0

我认为这取决于谁在数据网格中填充或停靠的面板水平内容对齐不适用于中心内容对齐您可以像这样使用 DataGrid.resource 标记

<DataGrid.Resources>
                <Style TargetType="DataGridCell">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                </Style>
</DataGrid.Resources>

或者看这个

 <DataGrid x:Name="DgvUsers" AutoGenerateColumns="False" IsReadOnly="True" EnableRowVirtualization="False"
                      HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" HeadersVisibility="Column"
                      IsTextSearchEnabled="True" FlowDirection="RightToLeft" SelectionMode="Single" FontFamily="/Sandogh.App;component/Font/#Lalezar" >
                <DataGrid.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="HorizontalAlignment" Value="Center"/>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="UserID" Visibility="Collapsed" Binding="{Binding Path=UserID}"/>
                    <DataGridTextColumn Header="FirstName" Binding="{Binding Path=Name}">
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock">
                                <Setter Property="HorizontalAlignment" Value="Center" />
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>
                    <DataGridTextColumn Header="LastName" Binding="{Binding Path=Family}"/>
                    <DataGridTextColumn Header="Gender" Binding="{Binding Path=TGender}"/>
                    <DataGridTextColumn Header="UserName" Binding="{Binding Path=UserName}"/>
                    <DataGridTextColumn Header="Password" Binding="{Binding Path=Password}"/>
                    <DataGridTextColumn Header="Activity" Binding="{Binding Path=TActivity}"/>
                    <DataGridTextColumn Header="JobID" Visibility="Collapsed" Binding="{Binding Path=JobID}"/>
                    <DataGridTextColumn Header="JobName" Binding="{Binding Path=JobName}"/>
                </DataGrid.Columns>
            </DataGrid>
于 2020-09-24T10:39:41.600 回答
0

就我而言,这(非常简单)工作正常:

<DataGrid
    x:Name="MyDataGrid"
    ...
    TextBlock.TextAlignment="Center">
</DataGrid>
于 2021-09-27T23:13:56.220 回答