我在这里遇到了一个我无法弄清楚的小 WPF 问题。
我正在尝试设置一个ResourceDictionary包含许多颜色、画笔和ControlTemplate条目的内容。
我的问题是我正在尝试将 a 的前景画笔设置为Label同一文件中的资源,并且该位不起作用并且没有关于它的跟踪消息。
<Color x:Shared="false" x:Key="DefaultForegroundColor" A="255" R="0" G="0" B="0" />
<!--<Color x:Shared="false" x:Key="DefaultForegroundColor" A="255" R="255" G="0" B="0" />-->
<SolidColorBrush x:Shared="false" x:Key="DefaultForegroundBrush" Color="{DynamicResource DefaultForegroundColor}"/>
<Color x:Shared="false" x:Key="HeadingColor" A="255" R="17" G="158" B="218" />
<SolidColorBrush x:Shared="false" x:Key="HeadingBrush" Color="{DynamicResource HeadingColor}"/>
<Style TargetType="{x:Type Label}" x:Key="SectionHeadingStyle">
<Setter Property="FontSize" Value="18"/>
<Setter Property="Foreground" Value="{DynamicResource HeadingBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border BorderThickness="0,0,0,2" BorderBrush="{DynamicResource DefaultForegroundBrush}" HorizontalAlignment="Left">
<ContentPresenter Margin="5"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
边框颜色可以正常工作(即使它不是黑色),但标签的前景仍然是黑色而不是浅蓝色。任何想法我做错了什么?
谢谢。
更新 忘了提到它在设计器中也很好看。
更新 2
在窥探可视化树时,Foreground颜色似乎在模板中占据了大部分位置,直到ContentPresenter使用 aTextBlock来显示标题文本。保持黑色,而上面的TextBlockContentPresenter 将漂亮的蓝色阴影设置为TextElement.Foreground. 我可以在模板中做些什么来赋予创建TextBlock的前景色吗?