我正在处理的wpf项目有一个小问题.我是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>
Run Code Online (Sandbox Code Playgroud)
现在我想知道如何覆盖某些样式属性,例如For Buttons我想要覆盖字体样式,同时保留其余的aero样式.
如果我在窗口资源中为按钮定义样式,例如
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}" x:Key="RedTextBasedOnButton">
<Setter Property="Foreground" Value="Red" />
</Style>
Run Code Online (Sandbox Code Playgroud)
并根据上述样式定义一个按钮
该按钮会丢失所有的航空风格属性.我认为问题与我为样式定义BasedOn属性的方式有关
BasedOn="{StaticResource {x:Type Button}}"
Run Code Online (Sandbox Code Playgroud)
我不认为资源是静态的,因为它来自一个DLL,它可能应该是这样的.不过不确定.
BasedOn="{DynamicResource {x:Type Button}}"
Run Code Online (Sandbox Code Playgroud)
但上面引发了异常,如果我在app.xaml中有多个resourcedictionary,例如luna和classic.如何判断哪一个用作默认值,同时使用和覆盖另一个(例如luna)专门用于我的ui中的某些控件?所以我的一些按钮将基于luna风格,而一些按照航空风格进行一些进一步修改?
任何的想法?
问候,
除了皇家主题风格,我希望每个按钮都有5分的余量.
Window1.xaml:
<Window x:Class="_styles.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Content="Button A"/>
<Button Content="Button B"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
它编译但我得到:
PresentationFramework.dll中出现未处理的"System.StackOverflowException"类型异常
public Window1() {
InitializeComponent(); // <-- getting exception here
}
Run Code Online (Sandbox Code Playgroud)
没有例外细节,因为:
{无法计算表达式,因为当前线程处于堆栈溢出状态.}