Search code examples
c#wpfheadergroupbox

GroupBox.Header not working


I am trying to add an image to my GroupBox Header like so:

<GroupBox>
    <GroupBox.Header>
        <StackPanel Orientation="Horizontal">
            <Label Content="Test Box" />
            <Image Source="MyImage.jpg" />                   
        </StackPanel>
    </GroupBox.Header>
    .....
    <!-- All my other code which I know works correctly -->
    .....
<GroupBox>

However, what shows up for my GroupBox Header is:

enter image description here

I see this method of modifying the GroupBox Header all over the web when I search on how to do this but I cannot get it to work. I even tried removing the Image in case that was causing an issue and it still give the same result.

What am I doing wrong here?


Solution

  • After reading Ganesh's post, I did a search for a GroupBox style and found that there was one in my project that I was unaware of. The GroupBox style was defined as follows:

    <!-- GroupBox & GroupBox.Header Style -->
    <Style x:Key="GroupBoxStyle" TargetType="{x:Type GroupBox}">
        <Setter Property="Margin" Value="5" />
        <Setter Property="Padding" Value="5" />
    
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding}" FontWeight="Bold"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <Style TargetType="{x:Type GroupBox}" 
           BasedOn="{StaticResource GroupBoxStyle}" />
    

    By changing my code to the following I was able to override the default GroupBox style:

        <GroupBox>
            <GroupBox.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Julius Caesar"/>
                        <Image Source="MyImage.jpg" /> 
                    </StackPanel>
                </DataTemplate>
            </GroupBox.HeaderTemplate>
            ......