tags:

views:

1211

answers:

3

I want every button to have 5 points margin, in addition to Royale theme style.

Window1.xaml:

<Window x:Class="_styles.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
  <Window.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
      </ResourceDictionary.MergedDictionaries>
      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Margin" Value="5"/>
      </Style>
    </ResourceDictionary>
  </Window.Resources>
  <StackPanel>
    <Button Content="Button A"/>
    <Button Content="Button B"/>
  </StackPanel>
</Window>

It compiles but I get:

An unhandled exception of type 'System.StackOverflowException' occurred in PresentationFramework.dll

public Window1() {
    InitializeComponent(); // <-- getting exception here
}

There are no exception details because:

{Cannot evaluate expression because the current thread is in a stack overflow state.}

A: 

I would remove the BasedOn attribute - it's not necessary. Think of it this way, merging the Royale theme will apply the button theme, and you just want to change the margin - styles are additive in nature, so it will combine the Royale theme and your own button theme without specifying the BasedOn attribute - does that make sense?

Cheers!

Charles
I was under impression that closer-scoped style *overwrites* previous style, i.e. at most one style can be applied. And BasedOn attribute is the way to *extend* a style.
Ah, you're absolutely right. That's interesting - I tried the suggestion I gave you using the Aero theme, and leaving out the BasedOn="..." resets the buttons style back to the original XP theme. Woops :) If you can share more of the source, perhaps I could take a look.
Charles
+1  A: 

This seems to be a circular reference between your style and the one defined in PresentationFramework.Royale. A workaroud would be to place resources at different levels:

<Window x:Class="_styles.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="Window1" Height="300" Width="300">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Margin" Value="5"/>
        </Style>
    </StackPanel.Resources>
    <Button Content="Button A"/>
</StackPanel>
</Window>
Stanislav Kniazev
A: 

Please see this question and my answer for another solution that doesn't require you to specify a resource dictionary in every window and allows you to resolve the BasedOn style dynamically.

Andre Luus