Override default styling in WPF TextBox, based on PresentationFramework.Aero

前端 未结 2 1480
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 13:50

I want to use the Aero textbox styling, but still override some properties. I try to accomplish this by:



        
相关标签:
2条回答
  • 2020-12-08 14:43

    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>
    
    0 讨论(0)
  • 2020-12-08 14:49

    Unlike the code in accepted answer this one allows using resource dictionary for styles. Shamelessly stolen from http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/ answered by 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}"/>
    
    0 讨论(0)
提交回复
热议问题