Search code examples
wpfxamlheaderstylesgroupbox

XAML: How do I make part of a GroupBox Header bold, with binding?


I've asked a question long ago and got help on it. The requirement has changed slightly.

Original question: XAML: How do I make part of a GroupBox Header bold?

The original requirement was to just hard-code the header text to:

Students (Max: 32)

The solution that Ed gave me was:

<GroupBox.Header>
    <TextBlock>
        <Span FontWeight="Bold">Students</Span>
        <Span>(Max: 32)</Span>
    </TextBlock>
</GroupBox.Header>

Now I need to bind both of these sections to some dependency properties, such that the header would appear as one of these:

  • Students (Max: 32)
  • Students (Max: 64)
  • Employees (Max: 32)
  • Employees (Max: 64)
  • etc...

I need to have binding to both of these Span sections, something akin to this:

<GroupBox.Header>
    <TextBlock>
        <Span FontWeight="Bold">{Binding ProfessionString}</Span>
        <Span>{Binding MaxString}</Span>
    </TextBlock>
</GroupBox.Header>

, but obviously the {Binding ProfessionString} and {Binding MaxString} will be displayed verbatim.

Is there a way to do that?

Thanks.


Solution

  • You should use Run elements and bind their Text properties to your source properties:

    <GroupBox.Header>
        <TextBlock>
            <Run FontWeight="Bold" Text="{Binding ProfessionString, Mode=OneWay}" />
            <Run Text="{Binding MaxString, Mode=OneWay}" />
        </TextBlock>
    </GroupBox.Header>