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>
Author by
Hossein Narimani Rad
Ph.D. Student in Geospatial Information Systems (GIS). LinkedIn GitHub
Updated on July 08, 2020Comments
-
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 singleGroupBox
:<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}>
WhichmyTemp
is a simpleDataTemplate
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 about 11 yearsYes, I tried something like this, But the designer throws exception
-
Hossein Narimani Rad about 11 yearsSo the GroupBox's header would be "Test Header" or "Test Templated Header"
-
DHN about 11 yearsDid you try what I posted? And what kind of exception is the designer throwing?
-
Hossein Narimani Rad about 11 yearsIf you have some
GroupBox
with different Headers what will happen? -
Hossein Narimani Rad about 11 yearsDid you changed your solution? I tried it and this time it works. Thank you.
-
DHN about 11 yearsNo I didn't. But I'm glad I could help.
-
JSJ about 11 yearsare you using any sort of binding in your datatemplate. if so please make it dynamicresource insteed of StaticResource.
-
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 about 11 yearsyes you are right it will not useful as we are using direct property values. as FontWeight="Bold" thanks for the cut.
-
l33t over 8 yearsThis breaks
HeaderStringFormat
functionality. -
Grault over 8 yearsThe accelerator (shortcut) key seems to be fixed by using a
Label
instead of aTextBlock
. -
user1151923 over 8 yearsI just changed
TextBlock
toLabel
and it worked. I had to setHeight="15"
for the label though because otherwise theGroupBox
's contents would look weirdo. -
DHN over 7 years@HristoYankov: Uhm, why?