首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GroupBox标头模板

GroupBox标头模板
EN

Stack Overflow用户
提问于 2013-12-29 13:34:21
回答 1查看 6.3K关注 0票数 0

我的Groupbox模板被定义为

代码语言:javascript
复制
<Style x:Key="{x:Type GroupBox}" TargetType="{x:Type GroupBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupBox}">
                <Border BorderThickness="1" BorderBrush="SomeColour" 
                        Background="SomeColour"
                        CornerRadius="4">
                    <Grid SnapsToDevicePixels="true">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>
                        <ContentPresenter Grid.Row="0" ContentSource="Header" 
                                          Margin="2"/>
                        <ContentPresenter Grid.Row="1"
                                          Margin="{TemplateBinding Padding}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

如果标头设置为一个简单的字符串,例如

代码语言:javascript
复制
<GroupBox Header="The header!" />

文字是粗体和一些默认的颜色?

我试过以下几种方法,但它只适用于重量,而不是颜色。

代码语言:javascript
复制
<ContentPresenter ContentSource="Header" TextBlock.Foreground="Red" 
                  TextBlock.FontWeight="Bold"/>

编辑:这是文本块样式

代码语言:javascript
复制
<Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="{StaticResource LabelForegroundBrush}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="1" />

        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{StaticResource DisabledLabelForegroundBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>

编辑2:如果我将以下内容放在<Window.Resources>中,它似乎有效,但是如果我将它们放在<Application.Resources>中,..失败了?

代码语言:javascript
复制
<XXX.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Green" />
        </Style>

        <Style x:Key="{x:Type GroupBox}" TargetType="{x:Type GroupBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupBox}">
                        <Grid SnapsToDevicePixels="true">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <ContentPresenter Grid.Row="0" ContentSource="Header" TextElement.Foreground="Red" />
                            <ContentPresenter Grid.Row="1" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </XXX.Resources>

用法:

代码语言:javascript
复制
<GroupBox Header="Header">
        <Button Content="Content" />
    </GroupBox>
EN

回答 1

Stack Overflow用户

发布于 2013-12-29 16:18:48

尝尝这个

代码语言:javascript
复制
 <Style x:Key="{x:Type GroupBox}" TargetType="{x:Type GroupBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupBox}">
                    <Border BorderThickness="1" BorderBrush="SomeColour" 
                    Background="SomeColour"
                    CornerRadius="4">
                        <Grid SnapsToDevicePixels="true">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Foreground="Red" FontWeight="Bold" Margin="2"> 
                            <ContentPresenter ContentSource="Header"/>
                            </TextBlock>
                            <ContentPresenter Grid.Row="1" Margin="{TemplateBinding Padding}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20825669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档