Text alignment in DataGrid
Asked Answered
S

10

65

I'm programming by WPF. I need a way to make center content of cells, in DataGrid control. I use this code also:

<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}"/>

What is wrong?

Sharell answered 11/8, 2013 at 22:2 Comment(2)
Duplicate: #6072593Chamberlin
Possible duplicate of Text alignment in a WPF DataGridHolocaust
O
139

You need set DataGridCell style

    <DataGrid>
        <DataGrid.Columns>
            <DataGridTextColumn>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
Optimum answered 12/8, 2013 at 3:15 Comment(7)
(to Lai32290)This is not my answer. Your answer add a column to my DataSet and do not set center even one of my cell contents.Sharell
This answer looks correct to me - how do you expect to know if the cell content is properly aligned without adding at least one column?Luganda
(to Babak) Sorry, that code realy not worked, I have edited my code, please try againOptimum
(to Lai32290)Thanks for your answer but it does not work. My contents are RTL (Right To Left). Is it issue?Sharell
@Sharell to Answer you RTL isn't the problem his code works like expectedFortier
I think we can be glad that with WPF not all elements are of type object and that we have to add each property via styles etc.Electrostriction
how to set that in the <Window.Resources> instead ?Unusual
E
31

For those who need to format only one dynamic DataGrid column in VB.NET from a custom XAML style:

In Application.xaml:

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

In VB.NET code:

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

Regards!

Elsey answered 14/3, 2016 at 19:56 Comment(0)
S
16

As mentioned in other answers:

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

HorizontalAlignment

This will affect any other styles such as background. To only center the text use this instead:

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

textalign

Sapphera answered 23/1, 2016 at 17:36 Comment(0)
N
10

Maybe just create a style:

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

Edited.

Nanji answered 12/8, 2013 at 9:32 Comment(4)
How use this in my code? please explain more. I am new in xamlHonor
You can add the style in the resource of your windowNanji
Not nice for me because then your textbox border/size maybe is not filling the cell size completely. So I prefer the answer from Lai32290Nourishment
@Nourishment a bit late, but this answer is much more effective than accepted answer if you need to do it on all your Grid cells. In my case I used that answer, and just added a style also on TextBlock, with also some margins to have a small space between TextBlock and Cell's bordersSyncopate
M
3

for affect all column

 <Window.Resources>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
 </Window.Resources>
Matrilocal answered 11/12, 2015 at 13:4 Comment(1)
It looks like this affects only Column Header. Also : <Setter Property="HorizontalAlignment" Value="Stretch"/> is not required (is the default) Do i miss something ?Chronologist
I
2

In my case this (very simple) is working fine:

<DataGrid
    x:Name="MyDataGrid"
    ...
    TextBlock.TextAlignment="Center">
</DataGrid>
Iconography answered 27/9, 2021 at 23:13 Comment(0)
O
1

In case you want to center the dates in a 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>
Outflank answered 27/3, 2014 at 8:0 Comment(0)
B
1

How to center text in 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>
Build answered 13/7, 2019 at 17:1 Comment(0)
V
0

I think it's depends on panel who is filled or docked in datagrid horizontal content alignment doesn't work for center content alignment you can use DataGrid.resource tag like this

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

or see this one

 <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>
Vittle answered 24/9, 2020 at 10:39 Comment(0)
I
0

This may be a more simpler solution.

<DataGrid>
    <!-- your code -->
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="HorizontalAlignment" Value="Center"/>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>
Immortality answered 11/4 at 20:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.