【发布时间】:2012-10-06 09:05:38
【问题描述】:
首先,我在应用程序资源中有一个样式作为资源,如下所示:
<Style x:Key="ContentTextBlock" TargetType="FrameworkElement">
<Setter Property="TextBlock.HorizontalAlignment" Value="Center"/>
<Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
<Setter Property="TextBlock.FontFamily" Value="Verdana"/>
<Setter Property="TextBlock.FontSize" Value="12"/>
<Setter Property="TextBlock.FontWeight" Value="Normal"/>
<Setter Property="TextBlock.Foreground" Value="Orange"/>
</Style>
我创建了一个自定义控件,其中包含标题和内容。 这是自定义控件模板中内容部分的外观:
<ContentPresenter Content="{TemplateBinding Content}"
Style="{DynamicResource ContentTextBlock}">
</ContentPresenter>
我的问题是,如果我像这样使用创建的控件:
<local:CutPage.Content>
<TextBlock>Header</TextBlock>
</local:CutPage.Content>
Fontsize、FontWeight 和 FontFamily 被 TextBlock 的显式或默认样式覆盖(我认为是这种情况,但我不确定)。我已阅读有关dependebcy 值顺序od 优先级的文章,但我怎么能猜出是什么覆盖了我的模板样式?我希望进入自定义控件的所有标题元素都使用这些值。我该怎么做?
附:有趣的是,一些样式设置器可以工作(比如将文本垂直和水平居中),而其他的则不行!
【问题讨论】: