基于 PresentationFramework.Aero 覆盖 WPF TextBox 中的默认样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/643440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Override default styling in WPF TextBox, based on PresentationFramework.Aero
提问by Inferis
I want to use the Aero textbox styling, but still override some properties. I try to accomplish this by:
我想使用 Aero 文本框样式,但仍会覆盖某些属性。我尝试通过以下方式实现这一点:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
</ResourceDictionary>
However, this results in a StackOverflowException
when starting my app.
When I remove the reference to PresentationFramework.Aero, this works but I get the default OS styling, which makes the app ugly. ;)
但是,这会在StackOverflowException
启动我的应用程序时导致 a 。当我删除对 PresentationFramework.Aero 的引用时,这有效,但我得到了默认的操作系统样式,这使应用程序变得丑陋。;)
So, in effect: if I want to override some style on all my textboxes I cannot get the Aero look. If I want the Aero look, I cannot override any styling. Deadlock.
因此,实际上:如果我想覆盖所有文本框上的某些样式,我将无法获得 Aero 外观。如果我想要 Aero 外观,我无法覆盖任何样式。僵局。
Any way to solve this?
有什么办法可以解决这个问题?
回答by Robert Macnee
It seems to work if you put the Style
as a lower-level resource, instead of in the same ResourceDictionary:
如果你把它Style
作为一个较低级别的资源,而不是放在同一个 ResourceDictionary 中,它似乎有效:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.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.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<Border BorderBrush="Blue" BorderThickness="3">
<Border.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
</Border.Resources>
<TextBox />
</Border>
</Grid>
回答by Mikhail
Unlike the code in accepted answer this one allows using resource dictionary for styles. Shamelessly stolen from http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/answered by Vivien Ruitz
与接受的答案中的代码不同,该代码允许使用资源字典作为样式。从http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/无耻地被盗,由Vivien Ruitz回答
<!--App.xaml-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyAppli;component/Resources/Themes/StyleDictionary.xaml"/>
<ResourceDictionary Source="/MyAppli;component/Resources/Themes/ApplyStyleDictionary.xaml"/>
...
</ResourceDictionary.MergedDictionaries>
<!--StyleDictionary.xaml-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="ButtonStyleToApply" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" >
... <!--Extend the aero style here-->
</Style>
<!--ApplyStyleDictionary.xaml-->
<Style TargetType="Button" BasedOn="{StaticResource ButtonStyleToApply}"/>