4

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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
THX-1138
  • 21,316
  • 26
  • 96
  • 160

3 Answers3

5

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
  • 5,386
  • 3
  • 35
  • 44
  • Can anyone explain how was this causing circular reference and why changing the scope has resolved it? – teenup Mar 07 '15 at 04:19
0

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.

Community
  • 1
  • 1
Andre Luus
  • 3,692
  • 3
  • 33
  • 46
0

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
  • 6,199
  • 6
  • 50
  • 66
  • 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. – THX-1138 May 29 '09 at 18:50
  • 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 Jun 02 '09 at 15:50