-1

I need to Style the WPF dataGrid headers AND bind the header data.

Binding works OK using:

<TextBlock Text="{Binding DataContext.HeadData, 
        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />

If I now add a header style to the DataGrid, the TextBlock in the style overrides the binding data.

I'm using all the latest Dot-Net and VS 2019.

Any idea how to marry the two requirements? It's consumed me!

Rich

Federico Rossi
  • 415
  • 5
  • 11
Ratatat Richie
  • 131
  • 1
  • 10
  • If I understood correctly, you should be able to do this: ``. The TextBlock should not be needed, since Header converts it automatically. I also think that in this scenario, all the RelativeSource stuff would not be needed, since you should still have the same DataContext as the DataGrid one. – Federico Rossi Mar 04 '21 at 09:16
  • Header="{Binding HeadData}" HeaderStyle="{StaticResource myStyle}"> Shows the style colours etc but no text from binding. Neither does the boring Ancestor version. The only way I can get bound data is using: However, if I add style it overrides the bindings. – Ratatat Richie Mar 04 '21 at 12:09

1 Answers1

1

Ok, I've tried it myself and this is the solution I've found:

You have to define the TextBlock inside the Style definition

<Style x:Key="MyHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                <TextBlock Text="{Binding DataContext.HeadData, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

In some way, setting an Header Style, and specifying an Header content gives conflict. So you have to move everything inside the style.

Obviously you use the Style just like this.

<DataGridTextColumn HeaderStyle="{StaticResource MyHeaderStyle}" />

Hope I've been helpful.

EDIT

This solution doesn't works (or else, it works for one column only DataGrid). In the comments I linked a solution given on a different question, that could be used here as well.

Federico Rossi
  • 415
  • 5
  • 11
  • Thanks a bunch. The only issue I see is that it works nicely for a one- column dataGrid, but I have a dozen columns. I would need a style for each column. Not a huge problem but typically with XAML very lumpy. Unless I'm probably missing something.. – Ratatat Richie Mar 04 '21 at 13:33
  • Do you have N properties that must be bound to N DataGridColumns' Header? I would go with AttachedProperties, if you need an example I can extend my answer with one. – Federico Rossi Mar 04 '21 at 13:57
  • Attached properties sounds like it could solve other 'lumpy' issues for me. Yes Please. – Ratatat Richie Mar 04 '21 at 14:35
  • I'm sorry, I wasn't able to make it works. Luckily I was able to find [this](https://stackoverflow.com/questions/16965615/how-to-bind-column-header-to-property-in-viewmodel-wpf-mvvm) solution, that worked in my tests. Hope it solves your problem. – Federico Rossi Mar 04 '21 at 20:15
  • Thanks Federico, Looks good I'll work on it soon. In the meantime your other solution is working for me. I think it should be the answer. – Ratatat Richie Mar 05 '21 at 21:42