wpf GroupBox 标题模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20825669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GroupBox header template
提问by pastillman
My Groupbox template is defined as so
我的 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>
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???
编辑 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>
Usage :
用法 :
<GroupBox Header="Header">
<Button Content="Content" />
</GroupBox>
回答by Heena Patil
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>
回答by Rohit Vats
You need attached properties TextElement.FontWeightand TextElement.Foregroundinstead.
您需要附加属性TextElement.FontWeight,TextElement.Foreground而不是。
<ContentPresenter ContentSource="Header"
Margin="2"
TextElement.FontWeight="Bold"
TextElement.Foreground="Red"/>
<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.
如果样式在应用程序资源下,它会忽略在 ContentPresenter 上设置的前景。如果我在 Window 资源下有它,它就可以正常工作。
However, you can override Style for TextBlock and set only Foregroundproperty. 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.
但是,您可以覆盖 TextBlock 的样式并仅设置Foreground属性。您也可以使用BasedOn. 将该样式放在 ContentPresenter 的资源下,以便它仅被您的 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>

