Text alignment in a WPF DataGrid

2020-02-08 10:36发布

问题:

How can I align the column data to center in a WPF DataGrid?

回答1:

It's hard to say without knowing specifics, but here's a DataGridTextColumn that is centered:

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


回答2:

If you are using DataGridTextColumn you can use the following code snippet:

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


回答3:

I started with huttelihut's solution. Unfortunately, that didn't work for me just yet. I tweaked his answer and came up with this (solution is to align the text to the right):

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

As you can see, I applied the style to a TextBlock, not the DataGridCell.

And then I had to set the Element style, not the Cell style.

ElementStyle="{StaticResource RightAligned}"


回答4:

+1 for Kent Boogaart. I ended up doing this, which makes the code slightly less cluttered (and enables me to use the alignment on several columns):

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


回答5:

Here's @MohammedAFadil's XAML answer, converted to C# code behind:

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

To apply the Style, set the CellStyle property of the DataGrid, e.g.

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


回答6:

Or in code behind:

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


回答7:

I ended up having problems with the cell being shifted and looking funky using the accepted answer. I know it's late, but hopefully my findings will help someone. I use:

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

rather than the CellStyle.



回答8:

Ok, I used the frameworkElement approach but there was a strange behavior when you try to highlight the row.

I've put another example of WPF Datagrid alignment in this thread!



回答9:

My favorite solution is:

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



回答10:

For me this one works well

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


回答11:

Thanks Danny Beckett for converting @MohammedAFadil's XAML answer, converted to C# code. All of my datagrids are set up dynamically, so I can change anything, whenever.

To set up an empty datagrid, with nothing in it and then just bind it to data, just take your 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
        });