问题描述
我想使用Aero文本框样式,但仍要覆盖某些属性。我尝试通过以下方式实现此目的:
I want to use the Aero textbox styling, but still override some properties. I try to accomplish this by:
<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>
但是,这会导致 StackOverflowException
启动我的应用。
当我删除对PresentationFramework.Aero的引用时,此方法有效,但是我得到了默认的OS样式,这使应用程序难看。 ;)
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. ;)
因此,实际上:如果我想在所有文本框中覆盖某些样式,则无法获得Aero外观。如果要使用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.
有什么办法解决这个问题?
Any way to solve this?
推荐答案
似乎如果将 Style
放在较低级别的资源中,而不是放在同一ResourceDictionary中,则可以正常工作:
It seems to work if you put the Style
as a lower-level resource, instead of in the same 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>
这篇关于根据PresentationFramework.Aero重写WPF TextBox中的默认样式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!