5

A simple window:

<Window x:Class="MyApp.MainWindow" xmlns="..." xmlns:x="...">

<Window.Resources>
    <Style TargetType="Grid">
        <Setter Property="Margin" Value="8"/>
    </Style>
</Window.Resources>

<Grid>
    <TextBox VerticalAlignment="Top" HorizontalAlignment="Left">Test</TextBox>
</Grid>

</Window>

It looks like this:

Now we remove Window.Resources:

<Window x:Class="MyApp.MainWindow" xmlns="..." xmlns:x="...">
<Grid>
    <TextBox VerticalAlignment="Top" HorizontalAlignment="Left">Test</TextBox>
</Grid>
</Window>

And add the style definition to App.xaml:

<Application x:Class="MyApp.App" xmlns="..." xmlns:x="..." StartupUri="View\MainWindow.xaml">
<Application.Resources>
    <Style TargetType="Grid">
        <Setter Property="Margin" Value="8"/>
    </Style>
</Application.Resources>
</Application>

Strangely, the TextBox now gets a padding:

Why?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Mikhail Orlov
  • 2,789
  • 2
  • 28
  • 38

2 Answers2

5

Implicit Styles for elements that do not derive from Control (i.e. Grid) will be applied to all instances of that control when placed in the Application resources. But they will not be applied to certain instances when the Style is placed any where else.

Effectively, elements inside ControlTemplate are except from implicit Styles for their type, unless that Style is defined in application resources.

Since Grid is not a control (i.e. it doesn't derive from Control), placing it's Style in the application resources will affect every Grid in your application. This includes Grids defined in the ControlTemplate of controls, like TextBox.

More information can be found here.

Community
  • 1
  • 1
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
  • @loxxy - Yeah, it's a subtle nuance that I have yet to find documentation for. – CodeNaked Aug 04 '11 at 16:51
  • Well, thank you! So, what is the best way to avoid this? Put the dictionary in a separate file? Don't set styles for Grid and Panel at all? – Mikhail Orlov Aug 04 '11 at 18:05
  • @Mikhail - Yeah, in general you wouldn't need to apply global implicit Styles for these types. But every case is different. – CodeNaked Aug 04 '11 at 18:09
  • @CodeNaked - I basically want to defeat this problem again: http://stackoverflow.com/questions/2803432/is-there-some-kind-of-a-common-wpf-stylesheet. Will you please be so kind to write some answer there? I don't understand why is there no common way to get "Microsoft look". – Mikhail Orlov Aug 04 '11 at 18:35
  • @Mikhail - Unfortunately, I think you'd have to define your Styles in your Window.Resources. If you have many windows you could merge the resources into your Window.Resources, so they are are only defined in one location. – CodeNaked Aug 04 '11 at 19:02
1

I suppose the default content of textbox contains a grid while placing the inner content. When in application resources, the TextBox styling occurs, the Grid Style also gets applied to the Grid inside the TextBox.

But when the same Grid style is applied in window resources (i.e after global styling occurs), it does not affect the Grid inside the TextBox.

loxxy
  • 12,990
  • 2
  • 25
  • 56