我正在WinXP上编写一个WPF应用程序,我用这样的vista主题覆盖了默认主题:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var themerd = new ResourceDictionary();
themerd.Source = new Uri(@"PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\themes/aero.normalcolor.xaml", UriKind.Relative);
Resources.MergedDictionaries.Add(themerd);
}
Run Code Online (Sandbox Code Playgroud)
它主要工作得很好.当我使用按钮等控件时:
<Button />
Run Code Online (Sandbox Code Playgroud)
样式看起来很好,但如果我使用具有不同样式的Button,如下所示:
<Button>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Width" Value="80" />
</Style>
</Button.Style>
</Button>
Run Code Online (Sandbox Code Playgroud)
该样式将使用标准的WinXP样式覆盖指定的主题样式,而不是在其上构建.这对我来说是非常有限的.有没有办法避免这个问题?
我使用标准的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>
Run Code Online (Sandbox Code Playgroud)
嗯......文本框的所有前景色都变成了红色.但是,所有文本框都会丢失主题样式.是的,我知道我应该添加"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>
Run Code Online (Sandbox Code Playgroud)
抛出异常.与此WPF相同:扩展主题的样式 - 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>
Run Code Online (Sandbox Code Playgroud)
在所有窗口和用户控件中,我必须明确设置
<UserControl.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Foreground" Value="Red" …Run Code Online (Sandbox Code Playgroud) 我已将PresentationFramework.Aero添加到我的App.xaml合并词典中,如...
<Application
x:Class="TestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
<ResourceDictionary
Source="pack://application:,,,/WPFToolkit;component/Themes/Aero.NormalColor.xaml" />
<ResourceDictionary
Source="/CommonLibraryWpf;component/ResourceDictionaries/ButtonResourceDictionary.xaml" />
<!-- Note, ButtonResourceDictionary.xaml is defined in an external class library-->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
我正在尝试稍微修改按钮的默认外观.我把这个样式放在我的ButtonResourceDictionary中:
<Style TargetType="Button">
<Setter Property="Padding" Value="3" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
Run Code Online (Sandbox Code Playgroud)
所有按钮现在都有正确的填充和粗体文本,但它们看起来是"经典",而不是"Aero".如何修复此样式,以便我的按钮看起来都是Aero,但也有这些微小的变化?我宁愿不必Style为每个按钮设置属性.
更新
我应该首先提到这一点,但如果我尝试使用BasedOn,如下所示,我得到一个StackOverflowException:
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
<Setter Property="Padding" Value="3" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
Run Code Online (Sandbox Code Playgroud)
这通常会起作用,但不会合并到Aero词典中.如果我对这些词典进行评论,则异常消失.
更新2
如果我添加一个x:Key属性并手动设置样式,它可以正常工作(带有填充和粗体的Aero样式),但正如我所说,我更喜欢该样式全局自动应用于所有按钮.
更新3
我刚刚发现了一种新的皱纹.在我的应用程序中,ButtonResourceDictionary.xaml被放置在类库中(即,在外部项目中).如果我将此文件移动到本地文件夹,一切正常.因此,问题似乎是引用各种外部资源字典引起的不良交互.我正在纠正我的App.xaml代码片段(上面),以反映ButtonResourceDictionary实际上是在外部定义的.