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

Inf*_*ris 31 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>
Run Code Online (Sandbox Code Playgroud)

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

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

有办法解决这个问题吗?

Rob*_*nee 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>
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的回答,但无论如何都要使用aero resourcedictionary和我自己在Application.Resources(App.xaml)中定义的样式吗?我真的不能把它放在例如每个窗口上.. (5认同)

Mik*_*ail 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}"/>
Run Code Online (Sandbox Code Playgroud)