Search code examples
c#wpfxamlheadergroupbox

Binding not working on GroupBox.HeaderTemplate


I created a custom header for my GroupBox like the following image:

Custom GroupBox

I want also to set a Binding on the Backgroud of this header, so I wrote the following code:

 <GroupBox Width="130"
                      Height="80"
                      BorderBrush="Black"
                      Margin="5"
                      Grid.Row="0"
                      Background="{Binding HColor}">
                <GroupBox.HeaderTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Black"
                                BorderThickness="1"
                                CornerRadius="3"
                                HorizontalAlignment="Stretch"
                                Width="70"
                                Margin="-2,0,-3,-1"
                                Height="20"
                                Background="{Binding HColor}">
                            <TextBlock Text="Hubs"
                                       Foreground="Black"
                                       FontWeight="DemiBold"
                                       HorizontalAlignment="Center" />
                        </Border>
                    </DataTemplate>
                </GroupBox.HeaderTemplate>
 </GroupBox>

After execution, I the GroupBox's Background is colord in the right way, but not the header !

Custom GroupBox

Is anyone able to explain to me why it's not working well ?


Solution

  • Solution Found !

    I had to make an internal binding in the Xaml, between the backgroud of the GroupBox and it's header. Like the followiing code:

      <GroupBox Name="HubGroupBox"
                          Width="130"
                          Height="80"
                          BorderBrush="Black"
                          Margin="5"
                          Grid.Row="0"
                          Background="{Binding HubColor}">
                    <GroupBox.HeaderTemplate>
                        <DataTemplate>
                            <Border Canvas.ZIndex="2"
                                    BorderBrush="Black"
                                    BorderThickness="1"
                                    CornerRadius="3"
                                    HorizontalAlignment="Stretch"
                                    Width="70"
                                    Margin="-2,0,-3,-1"
                                    Height="20"
                                    Background="{Binding ElementName=HubGroupBox, Path=Background}">
                                <TextBlock Text="Hubs"
                                           Foreground="Black"
                                           FontWeight="DemiBold"
                                           HorizontalAlignment="Center" />
                            </Border>
                        </DataTemplate>
                    </GroupBox.HeaderTemplate>
      </GroupBox>
    

    The result is:

    enter image description here