So I'm trying to override the default in a template defined in a ResourceDictionary. The default alignment is left, but I want the lefthand column header to be aligned to the right and the right column header to be aligned to the left. The rest of the data is aligned fine.
Currently, this is what I have:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Controls/CustomDataGrid.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="CellTextStyleR" TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="TextAlignment" Value="Right"/>
<Setter Property="Margin" Value="2,0,5,0"/>
</Style>
<Style x:Key="CellTextStyleL" TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="TextAlignment" Value="Left"/>
<Setter Property="Margin" Value="5,0,0,0"/>
</Style>
<Style x:Key="HeaderRight" TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource DataGridColumnHeaderStyle}">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="Margin" Value="2,0,5,0"/>
</Style>
<Style x:Key="HeaderLeft" TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource DataGridColumnHeaderStyle}">
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Margin" Value="5,0,0,0"/>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<DataGrid Grid.Row="0" Grid.Column="1" Style="{StaticResource CustomDataGridStyle}" ItemsSource="{Binding InputDataCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Input Name" Binding="{Binding Name}" Width="50*" ElementStyle="{StaticResource CellTextStyleR}" HeaderStyle="{StaticResource HeaderRight}"/>
<DataGridTextColumn Header="Input State" Binding="{Binding State}" Width="50*" ElementStyle="{StaticResource CellTextStyleL}" HeaderStyle="{StaticResource HeaderLeft}"/>
</DataGrid.Columns>
</DataGrid>
What happens instead is that the column text stays left aligned.
I've also tried defining directly in the datagrid, but that just kills all formatting...
Is there a way to override just the alignment and margins, without killing all other formatting?
Thank you for your help in advance!
For the cells try
Control.HorizontalAlignment
like this:For the headers you might need to remove the BasedOn. I can't see those styles in your question...