【发布时间】:2010-12-19 08:16:15
【问题描述】:
我正在使用标准的 WPF 主题 Aero.NormalColor.xaml。而且效果很好。但是对于整个应用程序,我想将文本框的前景色覆盖为红色。
我的第一次尝试是这样的
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Red" />
</Style>
</ResourceDictionary>
</Application.Resources>
嗯...文本框的所有前景色都变成红色。但是,所有文本框都失去了主题样式。是的,我知道我应该添加“BasedOn”。我的第二次尝试是在样式标签中添加“BasedOn”。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Red" />
</Style>
</ResourceDictionary>
</Application.Resources>
抛出异常。和这个WPF : Extend Theme's style - StackOverflowException一样@
最终,我实现了我的目标。
在 App.xaml 中
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/Aero.NormalColor.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在所有窗口和用户控件中,我必须明确设置
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Red" />
</Style>
</UserControl.Resources>
以上代码多次复制粘贴,不易维护。有谁知道如何通过一次将前景设置为红色来实现我的目标?
【问题讨论】: