31

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

<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>

However, this results in a StackOverflowException when starting my app. When I remove the reference to PresentationFramework.Aero, this works but I get the default OS styling, which makes the app ugly. ;)

So, in effect: if I want to override some style on all my textboxes I cannot get the Aero look. If I want the Aero look, I cannot override any styling. Deadlock.

Any way to solve this?

Inferis
  • 4,582
  • 5
  • 37
  • 47
  • In your comment to Roberts answer below you seem to hint that you got this working with top-level resourcedictionaries. Please share what you came up with. – Oskar Mar 17 '10 at 11:46

2 Answers2

39

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>
Robert Macnee
  • 11,650
  • 4
  • 40
  • 35
  • Great, that works. I put the styles in another ResourceDictionary at the same level of the external dictionary, and the StackOverflow disappeared. Thanks! – Inferis Mar 16 '09 at 08:46
  • 5
    Thanks for this answer, but is there anyway to get this to work with both the aero resourcedictionary and my own styles defined in Application.Resources (App.xaml)? I really can't afford to place this on for example every window.. – Oskar Mar 17 '10 at 11:44
12

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}"/>
Mikhail Poda
  • 5,742
  • 3
  • 39
  • 52