【发布时间】:2017-09-29 13:10:37
【问题描述】:
我有这样的按钮样式
<Style x:Key="NormalButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="ContentSite">
<Grid x:Name="ContainerGrid">
<ContentPresenter x:Name="Presenter" Style="{DynamicResource NormalButtonTextStyle}"/>
...
ContentPresenter 的样式在哪里
<Style x:Key="NormalButtonTextStyle" BasedOn="{StaticResource DefaultFontStyle}">
<Setter Property="TextElement.Foreground" Value="{DynamicResource NormalTextOnDarkBackgroundBrush}" />
...
然后我像这样定义了我的按钮
<Button Style="{StaticResource NormalButtonStyle}">
<Button.Content>
<TextBlock Text="Cancel"/>
...
但 TextBlock 并没有以 NormalButtonTextStyle 样式结束。我在运行时检查了元素,内容呈现器肯定有正确的文本前景,但随后子 TextBlock 最终继承自完全不同的东西并获得ControlTextBrush 的前景。
我是否误解了如何将样式应用于子元素?我应该如何定义按钮以便正确应用样式?
【问题讨论】:
-
您是否尝试在
ControlTemplate.Resources中包含ContentPresenter样式?