我的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:如果我将以下内容放在<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>
答案 0 :(得分:2)
试试这个
<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>
答案 1 :(得分:1)
您需要附加属性 TextElement.FontWeight
和TextElement.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>