How do I override the DataGridColumnHeader specified in my ResourceDictionary?

1.1k views Asked by At

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!

2

There are 2 answers

9
Glen Thomas On

For the cells try Control.HorizontalAlignment like this:

<Style x:Key="CellRightAlign">
    <Setter Property="Control.HorizontalAlignment"
            Value="Right" />
</Style>

<Style x:Key="CellLeftAlign">
    <Setter Property="Control.HorizontalAlignment"
            Value="Left" />
</Style>

<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 CellRightAlign}" HeaderStyle="{StaticResource HeaderRight}"/>
            <DataGridTextColumn Header="Input State" Binding="{Binding State}" Width="50*" ElementStyle="{StaticResource CellLeftAlign}" HeaderStyle="{StaticResource HeaderLeft}"/>
        </DataGrid.Columns>
    </DataGrid>

For the headers you might need to remove the BasedOn. I can't see those styles in your question...

0
sailorstar165 On

After much banging of my head on the keyboard, this is the solution I ended up with:

I changed the styles "HeaderLeft" and "HeaderRight" so that they weren't based on CustomDataGrid.xaml's DataGridColumnHeaderStyle anymore.

Instead, I copy-pasted what was inside DataGridColumnHeaderStyle into those styles and changed the settings I needed to change in there.

Not the elegant solution I was hoping for, but hey, if it works...