WPF DataGrid中的文本对齐方式

Pra*_*dda 52 .net c# wpf xaml wpftoolkit

如何将列数据与WPF中心对齐DataGrid

Moh*_*dil 90

如果您使用的是DataGridTextColumn,则可以使用以下代码段:

<Style TargetType="DataGridCell">
     <Style.Setters>
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
     </Style.Setters>
</Style>
Run Code Online (Sandbox Code Playgroud)

  • 这是最好的方法 - 在单元格上设置对齐会导致单元格不填充其宽度,这在选择行时看起来很奇怪 (7认同)

Ken*_*art 48

不知道具体细节很难说,但这是一个DataGridTextColumn集中的:

<wpf:DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True">
    <wpf:DataGridTextColumn.CellStyle>
        <Style>
            <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
        </Style>
    </wpf:DataGridTextColumn.CellStyle>
</wpf:DataGridTextColumn>
Run Code Online (Sandbox Code Playgroud)

  • 这种方法对我不起作用.文本居中,但单元格宽度不再与其标题匹配.穆罕默德的解决方案效果最好. (8认同)
  • 正如基夫所说,细胞也在发生变化。您必须像 Mohammed A. Fadil 一样瞄准 Textblock。 (2认同)

Jan*_*Jan 19

我从huttelihut的解决方案开始.不幸的是,这对我来说还不适用.我调整了他的答案并想出了这个(解决方案是将文本对齐):

<Resources>
    <Style x:Key="RightAligned" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Right"/>
    </Style>
</Resources>
Run Code Online (Sandbox Code Playgroud)

如您所见,我将样式应用于TextBlock,而不是DataGridCell.

然后我必须设置元素样式,而不是单元格样式.

ElementStyle="{StaticResource RightAligned}"
Run Code Online (Sandbox Code Playgroud)

  • @JP Hellemons:对我来说,它与`DataGridTextColumn`一起使用 (3认同)

T.J*_*aer 14

为Kent Boogaart +1.我最终做到了这一点,这使代码稍微混乱(并使我能够在几列上使用对齐):

<Resources>
      <Style x:Key="NameCellStyle" TargetType="DataGridCell">
                <Setter Property="HorizontalAlignment" Value="Center" />
      </Style>
</Resources>


<DataGrid.Columns>                           
   <DataGridTextColumn Header="Name" CellStyle="{StaticResource NameCellStyle}" Binding="{Binding Name}"/>                            
    // .. other columns        
</DataGrid.Columns>
Run Code Online (Sandbox Code Playgroud)

  • +1 样式,但对我来说 `Horizo​​ntalAlignment` 不起作用,而 `Horizo​​ntalContentAlignment` 起作用。 (2认同)

Dan*_*ett 9

这是@ MohammedAFadil的XAML答案,转换为C#代码:

var MyStyle = new Style(typeof(DataGridCell)) {
    Setters = {
        new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
    }
};
Run Code Online (Sandbox Code Playgroud)

要应用Style,请设置例如的CellStyle属性DataGrid

var MyGrid = new DataGrid() {
    CellStyle = MyStyle
};
Run Code Online (Sandbox Code Playgroud)


小智 5

或者在后面的代码中:

grid.CellStyle = newCellStyle();

public static Style newCellStyle()
{
    //And here is the C# code to achieve the above
    System.Windows.Style style = new Style(typeof(DataGridCell));
    style.Setters.Add(new System.Windows.Setter
    {
        Property = Control.HorizontalAlignmentProperty,
        Value = HorizontalAlignment.Center
    });
    return style;
}
Run Code Online (Sandbox Code Playgroud)


Rac*_*ael 5

我最终遇到了单元格移动的问题,并且使用接受的答案看起来很时髦。我知道已经晚了,但希望我的发现能对某人有所帮助。我用:

<DataGridTextColumn.ElementStyle>
     <Style>
          <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
     </Style>
Run Code Online (Sandbox Code Playgroud)

而不是单元格样式。