31

我想使用 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 外观,我无法覆盖任何样式。僵局。

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

4

2 回答 2

38

如果你把它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>
于 2009-03-13T17:18:09.530 回答
12

与已接受答案中的代码不同,此代码允许将资源字典用于样式。无耻地从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}"/>
于 2011-02-13T21:42:57.373 回答