GroupBox header template

自作多情 提交于 2019-12-11 19:08:11

问题


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>

回答1:


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>



回答2:


Try this

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


来源:https://stackoverflow.com/questions/20825669/groupbox-header-template

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