0

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!

sailorstar165
  • 151
  • 1
  • 11

2 Answers2

0

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...

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
  • The cells themselves are fine. It's the headers that are the problem. – sailorstar165 Aug 06 '15 at 17:29
  • Oh, the column headers? – Glen Thomas Aug 06 '15 at 17:32
  • Yeah. The column headers don't align the way I want with the current code, and anything else I try just wipes out all formatting in general with the column headers. Sorry if I wasn't clear in the problem description. I've edited it in an attempt to make it clearer. – sailorstar165 Aug 06 '15 at 18:24
  • Where is the style "DataGridColumnHeaderStyle"? I can't see it in your code. I guess it is in CustomDataGrid.xaml – Glen Thomas Aug 07 '15 at 09:19
  • There's one defined in the CustomeDataGrid.xaml that I'm trying to overwrite with the HeaderStyle at the end of the DataGridTextColumn lines. – sailorstar165 Aug 07 '15 at 11:41
  • If you want to override another style then don't used BasedOn as that will inherit the setters from the BasedOn style. If you don't used BasedOn you will completely replace all setters from other styles that would have been applied otherwise. – Glen Thomas Aug 07 '15 at 11:57
  • If you show me DataGridColumnHeaderStyle it might help me figure out your problem – Glen Thomas Aug 07 '15 at 11:58
  • The problem is, I want all the other settings from that DataGridColumnHeaderStyle definition... and it's a massive block of code that I don't want to have copied over if I'm using the resource dictionary... – sailorstar165 Aug 07 '15 at 15:35
  • The HeaderRight style works ok for me, so I have to assume that your DataGridColumnHeaderStyle is causing the problem. – Glen Thomas Aug 07 '15 at 16:04
0

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...

sailorstar165
  • 151
  • 1
  • 11