views:

488

answers:

2

Hello all!

I declared a style that I want to apply to ALL the buttons in the project, the style resides in the ResourceDictionary:

<Style TargetType="StackPanel">
    <Setter Property="Orientation" Value="Horizontal" />
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>

Now, in some window, I want to inherit from this style but add a value:

<Style TargetType="StackPanel"> 
    <Setter Property="Margin" Value="5"/>
</Style>

The problem is that it doesn't inherit from the global style, in order to inherit I have to assign a key to the global style:

<Style TargetType="StackPanel" x:Key="StackPanelStyle" />

And then in the window's XAML inherit (or/and override - optional) it:

<Style TargetType="StackPanel" BasedOn="StackPanelStyle" />

The problem is that if you assign a key, it's not global and you have to call it on each window/scope.

A solution to my question should be one of the two (is there anything else I am missing out?):

  1. Have a global style with a key, that is automatically applied to all its targeted controls in the entire application.
  2. A way to refer to the ResourceDictionary-level unnamed style without and override it.

I thought about redeclaring the styles near the named styles (in the ResourceDictionary) which actually works:

<!--In the ResourceDictionary-->
<Style x:Key="StackPanelStyle" TargetType="StackPanel">
    <Setter Property="Orientation" Value="Horizontal" />
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<!--In the app.xaml-->
<Style TargetType="StackPanel" BasedOn="{StaticResource StackPanelStyle}"/>
<!--In the window/page scope-->
<Style TargetType="StackPanel" BasedOn="{StaticResource StackPanelStyle}"/

But I am looking for better stuff than stupidly redeclare all the styles.

A: 

I would suggest that what you may be looking for here is a master style or behavior scenario that is generally achieved by creating a user control. If you were to create a new button control with your 'global' style applied to it, then anywhere you would use that control you could simply add any 'New styles or override styles if / when they were needed.

If you haven't created a user control, they are easy to implement. Good tutorial here.

PortageMonkey
+5  A: 

Try this:

<Style TargetType="{x:Type StackPanel}" BasedOn="{StaticResource {x:Type StackPanel}}">
  <!-- ... -->
</Style>

I have declared my base styles in a ResourceDictionary in App.xaml, if i override them in a specific window like this, it usually works.

Botz3000
Thanks buddy, that was short, quick and accurate!
Shimmy