1

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 by KM499

2
Contributors
4
Replies
18
Views
3 Years
Discussion Span
Last Post by KM499
Featured Replies
  • 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 }; Read More

  • 1

    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 ? Read More

  • 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. … Read More

  • 1

    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. Read More

1

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
};
1

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 ?

1

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 by Ketsuekiame

1

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.