我使用的是标准的WPF主题Aero.NormalColor.xaml。它工作得很好。然而,对于在整个应用程序,我想重写文本框的前景色为红色。
I am using the standard WPF theme Aero.NormalColor.xaml. And it works very well. However for the whole application, I would like to override the Foreground color of textboxes to red.
我的第一次尝试是,
<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>
唉...文本框的所有前景颜色变成红色。但是,所有的文本框失去了主题风格。是的,我知道我应该加上支持算法FMP。我的第二次尝试是在样式标记添加支持算法FMP。
Well... all foreground color of textboxes become red. However all textboxes lose the theme style. Yes, I know I should add "BasedOn". My second try is add "BasedOn" in the style tag.
<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:扩展主题的风格 - StackOverflowException
最后,我通过这个实现我的目标。
Eventually, I achieve my goal by this.
在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>
而在所有窗口和用户控制,我必须明确地设置
And in all windows and user control, I had to explicitly set
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Red" />
</Style>
</UserControl.Resources>
上面code是复制和粘贴多次,这是不容易的维护。有谁知道如何实现我的目标,只需设置前景色为红色由一次
我想你可以添加风格
到的ResourceDictionary
和合并与Aero主题是这样的:
I think you can add the Style
to a ResourceDictionary
and merging that with the Aero theme like this:
<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>
<!-- Adding the style to a resource dictionary -->
<ResourceDictionary>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Red" />
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这应该给您所有的文本框的红色前景色,而无需显式指定每个窗口和用户控制。
This should give ALL your textboxes red foreground color without having to explicitly specify that on each window and user control.