GroupBox标题模板 [英] GroupBox header template

查看:108
本文介绍了GroupBox标题模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Groupbox模板被定义为如此

 < Style x:Key ={x:Type GroupBox}TargetType ={x:Type GroupBox}> 
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type GroupBox}>
< Border BorderThickness =1BorderBrush =SomeColour
背景=SomeColour
CornerRadius =4>
< Grid SnapsToDevicePixels =true>
< Grid.RowDefinitions>
< RowDefinition Height =auto/>
< RowDefinition Height =auto/>
< /Grid.RowDefinitions>
< ContentPresenter Grid.Row =0ContentSource =Header
Margin =2/>
< ContentPresenter Grid.Row =1
Margin ={TemplateBinding Padding}/>
< / Grid>
< / Border>
< / ControlTemplate>
< / Setter>
< / style>

如何使它成为一个简单的字符串, p>

 < GroupBox标题=标题! /> 

文字粗体并且有一些默认颜色?

我尝试了以下方法,但它仅适用于体重,而不是颜色。

 < ContentPresenter ContentSource = HeaderTextBlock.Foreground =Red
TextBlock.FontWeight =Bold/>

编辑:这是textblock样式

 < Style TargetType ={x:Type TextBlock}> 
< Setter Property =ForegroundValue ={StaticResource LabelForegroundBrush}/>
< Setter Property =BackgroundValue =Transparent/>
< Setter Property =VerticalAlignmentValue =Center/>
< Setter Property =Horizo​​ntalAlignmentValue =Stretch/>
< Setter Property =MarginValue =1/>

< Style.Triggers>
<触发属性=IsEnabled值=假>
< Setter Property =ForegroundValue ={StaticResource DisabledLabelForegroundBrush}/>
< /触发>
< / style>编辑2:如果我将以下内容放在< Window.Resources>中,请将以下内容放置在 > 它似乎工作,但如果我把它们放在< Application.Resources> ,..它失败???

 < XXX.Resources> 
< Style TargetType ={x:Type TextBlock}>
< Setter Property =ForegroundValue =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 =0ContentSource =HeaderTextElement.Foreground =Red/>
< ContentPresenter Grid.Row =1/>
< / Grid>
< / ControlTemplate>
< / Setter>
< / style>

用法:

 < GroupBox Header =Header> 
< Button Content =Content/>
< / GroupBox>


解决方案

您需要附加属性 TextElement.FontWeight TextElement.Foreground 改为

 < ContentPresenter ContentSource =Header
Margin =2
TextElement.FontWeight =Bold
TextElement.Foreground =Red/>







如果样式位于应用程序资源下,则
会忽略ContentPresenter上设置的前景。如果我将它放在Window资源下,它就可以工作


但是,您可以覆盖TextBlock的Style并只设置 Foreground 属性。您也可以继承使用 BasedOn 在App资源下声明的所有其他属性。将该样式置于ContentPresenter的资源下,以便它仅在您的ContentPresenter中被覆盖。

这是可行的:

 < ContentPresenter Grid.Row = 0ContentSource =Header
Margin =2TextBlock.FontWeight =Bold>
< ContentPresenter.Resources>
< Style TargetType =TextBlock
BasedOn ={StaticResource {x:Type TextBlock}}>
< Setter Property =ForegroundValue =Red/>
< / style>
< /ContentPresenter.Resources>
< / ContentPresenter>


My Groupbox template is defined as so

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

How do I make it so that if the Header is set to a simple simple string, e.g

<GroupBox Header="The header!" />

The text is bold and with some default colour?

I tried the following, but it only works for the weight, not the colour.

<ContentPresenter ContentSource="Header" TextBlock.Foreground="Red" 
                  TextBlock.FontWeight="Bold"/>

Edit : here is the textblock style

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

Edit 2 : If I place the following in <Window.Resources> it seems to work, yet if I place them in <Application.Resources>, .. it fails???

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

Usage :

<GroupBox Header="Header">
        <Button Content="Content" />
    </GroupBox>

解决方案

You need attached properties TextElement.FontWeight and TextElement.Foreground instead.

<ContentPresenter ContentSource="Header"
                  Margin="2"
                  TextElement.FontWeight="Bold"
                  TextElement.Foreground="Red"/>


In case style is under Application resources, it ignores the foreground set on ContentPresenter. If i have it under Window resources, it works fine.

However, you can override Style for TextBlock and set only Foreground property. Also you can inherit all other properties from Style declared under App resources using BasedOn. Place that style under resource of ContentPresenter so that it gets overridden only for your ContentPresenter.

This will work:

<ContentPresenter Grid.Row="0" ContentSource="Header" 
                  Margin="2" TextBlock.FontWeight="Bold">
    <ContentPresenter.Resources>
       <Style TargetType="TextBlock"
              BasedOn="{StaticResource {x:Type TextBlock}}">
           <Setter Property="Foreground" Value="Red"/>
       </Style>
     </ContentPresenter.Resources>
</ContentPresenter>

这篇关于GroupBox标题模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆