【问题标题】:GroupBox header templateGroupBox 标题模板
【发布时间】:2013-12-29 13:34:21
【问题描述】:

我的 Groupbox 模板是这样定义的

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

如果 Header 设置为一个简单的简单字符串,我该如何做,例如

<GroupBox Header="The header!" />

文本是粗体并带有一些默认颜色?

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

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

编辑:这里是文本块样式

<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:如果我将以下内容放在 &lt;Window.Resources&gt; 中,它似乎可以工作,但如果我将它们放在 &lt;Application.Resources&gt; 中,.. 它会失败???

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

用法:

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

【问题讨论】:

    标签: wpf templates header styles foreground


    【解决方案1】:

    试试这个

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

    您需要附加属性 TextElement.FontWeightTextElement.Foreground

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


    如果样式在应用程序资源下,它 忽略 ContentPresenter 上设置的前景。如果我在 Window 资源下有它,它可以工作 很好。

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

    这将起作用:

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

    【讨论】:

    • 这似乎也不起作用。它似乎仍然采用了我为我的 TextBlock 模板定义的前景色。
    • 文本块模板?你也可以发一下吗? AFAIK,TextBlock 没有 Template 属性。
    • 对不起,我说的是Textblock风格,我贴在上面
    • 这仍然不是问题。如果这是一个问题,您能否通过从 Style 中删除 Setter of Foreground 来确认?但我认为它不会有任何影响。
    • 如果我删除 TextBlock 中的前景设置器,它可以工作,但我真的不想这样做。我想也许我根本不应该在我的主题中使用隐式文本块样式?
    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2015-05-14
    • 2016-12-04
    • 2011-06-21
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多