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

风流意气都作罢 提交于 2019-12-10 19:39:18

问题


I need to make the first part of a GroupBox Header bold, and the other part non-bold. Here is the goal I'm trying to achieve:

Students (Max: 32)

        <GroupBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" Margin="40, 80, 40, 80">
        <GroupBox.Header>
            <Span FontWeight="Bold">Students</Span>
            (Max: 32)
        </GroupBox.Header>
        <StackPanel>
        ...

This gives me the error: The property "Header" is set more than once.

I know that it works for TextBlocks, but I can't make it happen for GroupBox Headers:

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

Thanks.


回答1:


GroupBox.Header can only contain one thing, but that one thing can be just about anything you like. It can be a TextBlock with partially bold content, for one convenient example:

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

In XAML, many controls have properties called Content or Header, which are of type Object. You can always give those a string, but you can also use any kind of templated or formatted content you like. In Windows Forms your menu item headers or groupbox headers had to be strings, unless you were in the mood to spend real time messing around with owner-draw code. In XAML, they can be anything XAML can render, to the point of ridiculousness:

<GroupBox>
    <GroupBox.Header>
        <GroupBox Header="I Heard You Like GroupBox Headers">
            LOL
        </GroupBox>
    </GroupBox.Header>
    <GroupBox Header="Content Box">Content</GroupBox>
</GroupBox>

It's like an infinitely recursive Swiss Army knife where every blade is also a Swiss Army knife, just like its parent.




回答2:


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


来源:https://stackoverflow.com/questions/41270164/xaml-how-do-i-make-part-of-a-groupbox-header-bold

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!