How to use style for GroupBox header?

29,115

Solution 1

Did you try the following?

<Style TargetType="GroupBox">
    <Setter Property="BorderBrush" Value="{StaticResource lightBlueBrush}"/>
    <Setter Property="Margin" Value="25,1,5,5"/>
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding}" FontWeight="Bold"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

Usage:

<GroupBox Header="Title" />

Solution 2

A groupboxs headerTemplate is a type of DataTemplate. so you should provide a datatemplate object insteed of style or template.

try below one.

   <Window.Resources>

        <DataTemplate x:Key="DataTemplate1">
            <TextBlock Text="Test Templated Header"/>
        </DataTemplate>

    </Window.Resources>
    <Grid>
        <GroupBox  Header="Test Header" HeaderTemplate="{StaticResource DataTemplate1}">
            <Border BorderBrush="Red" Margin="10">
                <TextBlock Text="Hello"/>
            </Border>
        </GroupBox>
    </Grid>
Share:
29,115
Hossein Narimani Rad
Author by

Hossein Narimani Rad

Ph.D. Student in Geospatial Information Systems (GIS). LinkedIn   GitHub

Updated on July 08, 2020

Comments

  • Hossein Narimani Rad
    Hossein Narimani Rad almost 4 years

    I have lost of GroupBox in my form that their header text must be Bold. I know how to do it for a single GroupBox:

    <GroupBox>
         <GroupBox.Header>
             <TextBlock Text="HEADER TEXT" FontWeight="Bold"/>
         </GroupBox.Header>
    </GroupBox> 
    

    But I'm interested to know how to do it with Styles. Here is what I have tried:

    <Style TargetType="GroupBox">
        <Setter Property="BorderBrush" Value="{StaticResource lightBlueBrush}"/>
        <Setter Property="Margin" Value="25,1,5,5"/>
        //<Setter ??
    </Style>
    

    I have tried <Setter Property="HeaderTemplate" Value={StaticResource myTemp}> Which myTemp is a simple DataTemplate But VS suddenly closed! I'm not sure if I'm in the correct way of doing it, so anyone could help me?

    EDIT: Please test your idea before posting it as an answer!

  • Hossein Narimani Rad
    Hossein Narimani Rad about 11 years
    Yes, I tried something like this, But the designer throws exception
  • Hossein Narimani Rad
    Hossein Narimani Rad about 11 years
    So the GroupBox's header would be "Test Header" or "Test Templated Header"
  • DHN
    DHN about 11 years
    Did you try what I posted? And what kind of exception is the designer throwing?
  • Hossein Narimani Rad
    Hossein Narimani Rad about 11 years
    If you have some GroupBox with different Headers what will happen?
  • Hossein Narimani Rad
    Hossein Narimani Rad about 11 years
    Did you changed your solution? I tried it and this time it works. Thank you.
  • DHN
    DHN about 11 years
    No I didn't. But I'm glad I could help.
  • JSJ
    JSJ about 11 years
    are you using any sort of binding in your datatemplate. if so please make it dynamicresource insteed of StaticResource.
  • DHN
    DHN about 11 years
    -1 Sorry, but it's simply wrong, what you're suggesting. I don't see, how DynamicResource could help here. You're whole post is kind of misleading.
  • JSJ
    JSJ about 11 years
    yes you are right it will not useful as we are using direct property values. as FontWeight="Bold" thanks for the cut.
  • l33t
    l33t over 8 years
    This breaks HeaderStringFormat functionality.
  • Grault
    Grault over 8 years
    The accelerator (shortcut) key seems to be fixed by using a Label instead of a TextBlock.
  • user1151923
    user1151923 over 8 years
    I just changed TextBlock to Label and it worked. I had to set Height="15" for the label though because otherwise the GroupBox's contents would look weirdo.
  • DHN
    DHN over 7 years
    @HristoYankov: Uhm, why?