Reputation: 67
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.
Upvotes: 0
Views: 57
Reputation: 155
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>
Upvotes: 1