基于PresentationFramework.Aero覆盖WPF TextBox中的默认样式

时间:2009-03-13 16:08:42

标签: wpf resources aero

我想使用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>

但是,这会在启动我的应用时产生StackOverflowException。 当我删除对PresentationFramework.Aero的引用时,这可以工作,但我得到了默认的操作系统样式,这使得应用程序很难看。 ;)

所以,实际上:如果我想在我的所有文本框上覆盖一些样式,我就无法获得Aero外观。如果我想要Aero外观,我无法覆盖任何样式。死锁。

有什么方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:35)

如果将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>

答案 1 :(得分:12)

与接受答案中的代码不同,这个允许使用资源字典来表示样式。被 Vivien Ruitz回答的http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/无耻地偷走

<!--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}"/>