WPF中的覆盖主题

6

我正在进行一个WPF项目,遇到了一些小问题。由于我是新手,所以在我的app.xaml文件中,我使用了Microsoft Aero主题来设计我的应用程序。我在app.xaml文件中有如下代码:

 <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/Aero.NormalColor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

现在我想知道如何覆盖某些样式属性,例如对于按钮,我想覆盖字体样式,同时保留其余的 Aero 样式。

如果我在窗口资源中定义了一个按钮样式:

 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
            <Setter Property="Foreground" Value="Red" />
        </Style>

根据上述样式定义一个按钮

该按钮失去了所有的Aero样式属性。我认为问题在于我定义样式的BasedOn属性的方式。

BasedOn="{StaticResource {x:Type Button}}"

我认为这个资源不是静态的,因为它来自一个dll文件,可能应该像这样。但我不确定。
BasedOn="{DynamicResource {x:Type Button}}"

但是上面的代码会抛出异常,如果我的app.xaml中有多个ResourceDictionary,例如Luna和Classic,我该如何告诉它哪一个是默认的,同时在我的UI中使用并覆盖另一个(例如Luna),以便某些按钮基于Luna风格,而其他一些按钮基于Aero风格,并进行一些进一步的修改?
有什么想法吗?
问候,

可能是 覆盖被覆盖的 WPF 主题 的重复问题。 - Qantas 94 Heavy
任何一种样式都有你可以捆绑的显式键吗? - Killnine
  1. 动态链接 DLL 与动态链接资源无关。
  2. BasedOn 必须始终是静态资源。
- Kux
1个回答

0

针对您的具体问题:

例如,对于按钮,我想覆盖字体样式,同时保留其余的Aero样式。

我建议您在资源字典中添加以下内容:

<FontFamily x:Key="YourFont">Tahoma</FontFamily>

然后在你的样式中使用它:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
     <Setter Property="FontFamily" Value="{StaticResource YourFont}"/>
</Style>

因此,您可以根据一个单一的键来更改字体系列。

我建议查看这个http://wpfthemes.codeplex.com/SourceControl/latest#WPF.Themes.Demo/Window1.xaml


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接