组框标题模板



我的组框模板是这样定义的

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

我该如何使标头设置为简单的简单字符串,例如

<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 :如果我将以下内容放在<Window.Resources>它似乎有效,但如果我将它们放在<Application.Resources>中,.. 它会失败???

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

试试这个

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

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

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


如果样式在应用程序资源下,则 忽略在内容演示器上设置的前台。如果我在窗口资源下有它,它可以工作 好。

但是,您可以覆盖文本块的样式并仅设置Foreground属性。还可以使用 BasedOn 从"应用资源"下声明的"样式"继承所有其他属性。将该样式放在 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>

相关内容

  • 没有找到相关文章

最新更新