Hi All,

I am creating a dynamic GroupBox through C# and want to change only the header font and not all the fonts in that.

GroupBox myGroupBox = new GroupBox();
myGroupBox.Header = mychk.Content.ToString();
myGroupBox.Content = myStack;

Here, myGroupBox.FontWeight changes all the contents inside the myGroupBox but how to change only the header font of this myGroupBox ?

Edited 3 Years Ago by KM499

Make the header a seperate control and change the font of that control.

Label headerLabel = new Label
{
    Text = mychk.Content.ToString(),
    FontWeight = FontWeight.Bold
};

GroupBox myGroupBox = new GroupBox
{
    myGroupBox.Header = headerLabel,
    myGroupBox.Content = myStack
};

Thanks a lot Ketsuekiame,

However I am populating these things in a combobox with headings and I want to make this header text non-selectable in the combobox like we do using GroupStyle...How can I achieve this ?

Set a property such as IsHeader on the object you're adding to true. This will act as a marker for your style trigger.

MyObject cbiHeader = new MyObject { Text = "Header 1", IsHeader = true };

You also need to set up a style trigger based on your databinding.

So if you have a ComboBox object in your WPF...

<ComboBox ItemsSource="{Binding}" DisplayMemberPath="Text">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="MyObject">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsHeader}" Value="True">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Setter Property="FontWeight" Value="Bold"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

Edited 3 Years Ago by Ketsuekiame

Actually I didn't have ComboBox in my xaml but anyways I set the property of that object in the code behind as IsEnabled="false" and that worked.

Thanks for your time and replies..much appreciated.

This question has already been answered. Start a new discussion instead.