0

I have a DataGrid in my WPF window and I want the align the text to vertical center in the cells and - no go. Here are my styling properties to set the style of the datagrid cells but obviously the VerticalAlignment is not working

<Window.Resources>
    <Style TargetType="DataGridCell">
        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
        <Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
        <Setter Property="TextBlock.FontSize" Value="14"/>
        <Setter Property="Height" Value="50"/>
    </Style>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
    </Style>
</Window.Resources>

And here is my DataGrid

 <DataGrid x:Name="WOgrid" HorizontalAlignment="Center" Height="246" Margin="10,23.2,0,0" VerticalAlignment="Top" Width="119" IsReadOnly="True" 
              AutoGenerateColumns="False" Grid.Row="1" HeadersVisibility="Column" CanUserResizeRows="False" SelectionMode="Single">

        <DataGrid.Columns>

            <DataGridTextColumn Binding="{Binding WOnum}" Header="WO Number" Width="*"/>

            <DataGridTextColumn Visibility="Hidden" Binding="{Binding WOstatus}" Header="Status" Width="0"/>

        </DataGrid.Columns>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="VerticalContentAlignment" Value="Center"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding WOstatus}" Value="0">
                        <Setter Property="Background" Value="Orange"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding WOstatus}" Value="1">
                        <Setter Property="Background" Value="LightYellow"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding WOstatus}" Value="2">
                        <Setter Property="Background" Value="YellowGreen"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding WOstatus}" Value="3">
                        <Setter Property="Background" Value="GreenYellow"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding WOstatus}" Value="4">
                        <Setter Property="Background" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

    </DataGrid>

I tried the suggestion and seems like when HorizontialAlignment is there alone it works but if it's there with VerticalAlignment - nope.

Matthew
  • 67
  • 6
  • Possible duplicate of [DataGrid row content vertical alignment](https://stackoverflow.com/questions/3981250/datagrid-row-content-vertical-alignment) – ASh Oct 26 '19 at 11:39
  • I have tried this. Seems like when I set the Horizontal alignment the vertical alignment is cancelled. – Matthew Oct 26 '19 at 12:05

1 Answers1

1

You need to edit the DataGridCell template:

<Style TargetType="DataGridCell">
    <Setter Property="TextBlock.FontSize" Value="14"/>
    <Setter Property="Height" Value="50"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Florin Bratan
  • 145
  • 1
  • 6