WPF DataGrid中的文本对齐方式

问题描述 投票:49回答:11

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

c# .net wpf xaml wpftoolkit
11个回答
46
投票

如果不知道细节,很难说,但这里是一个居中的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>

0
投票

感谢Danny Beckett转换@ MohammedAFadil的XAML答案,转换为C#代码。我的所有数据网格都是动态设置的,所以我可以随时更改任何内容。

要设置一个空的数据网格,其中没有任何内容,然后将其绑定到数据,只需要使用datagrid.columns

        var centerTextSetter = new Style(typeof(DataGridCell))
        {
            Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center) }
        };
        DgDbNames.Columns.Add(new DataGridTextColumn()
        {
            Header = "Db Name",
            Binding = new System.Windows.Data.Binding("DbName"),
            IsReadOnly = true,
            Width = new DataGridLength(0.2, DataGridLengthUnitType.Star),
            CellStyle = centerTextSetter
        });

0
投票

对我来说这个很好用

<DataGridTextColumn Width="1*" Binding="{Binding Balance, StringFormat=C} "Header="Balance">
  <DataGridTextColumn.CellStyle>
      <Style>
        <Setter Property="TextBlock.TextAlignment"  Value="Right"/>
      </Style>
   </DataGridTextColumn.CellStyle>
 </DataGridTextColumn>

89
投票

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

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

19
投票

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

<Resources>
    <Style x:Key="RightAligned" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Right"/>
    </Style>
</Resources>

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

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

ElementStyle="{StaticResource RightAligned}"

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>

10
投票

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

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

要应用Style,请设置CellStyleDataGrid属性,例如

var MyGrid = new DataGrid() {
    CellStyle = MyStyle
};

4
投票

或者代码背后:

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

3
投票

我最终遇到了细胞被移位的问题,并且使用接受的答案看起来很时髦。我知道现在已经很晚了,但希望我的发现可以帮助别人。我用:

<DataGridTextColumn.ElementStyle>
     <Style>
          <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
     </Style>

而不是CellStyle。


1
投票

好吧,我使用了frameworkElement方法,但是当你试图突出显示该行时,会出现一种奇怪的行为。

我在这个thread中添加了WPF Datagrid对齐的另一个例子!


1
投票

我最喜欢的解决方案:

<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
        <Style>
                <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
        </Style>
</DataGridTextColumn.CellStyle>

© www.soinside.com 2019 - 2024. All rights reserved.