代码之家  ›  专栏  ›  技术社区  ›  Inferis

基于PresentationFramework.Aero重写WPF TextBox中的默认样式

  •  31
  • Inferis  · 技术社区  · 17 年前

    <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

    2 回复  |  直到 17 年前
        1
  •  38
  •   Robert Macnee    17 年前

    如果你把它放在 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>
    
        2
  •  12
  •   Mikhail Poda    15 年前

    与公认答案中的代码不同,此代码允许使用样式资源字典。无耻地从 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}"/>
    
    推荐文章