WPF:扩展主题的样式 - StackOverflowException

时间:2009-05-29 17:43:00

标签: wpf themes styles

除了皇家主题风格之外,我希望每个按钮都有5个点的余量。

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>

它编译但我得到:

  

PresentationFramework.dll中出现未处理的“System.StackOverflowException”类型异常

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

没有例外细节,因为:

  

{无法计算表达式,因为当前线程处于堆栈溢出状态。}

3 个答案:

答案 0 :(得分:5)

这似乎是您的样式与PresentationFramework.Royale中定义的样式之间的循环引用。一个可行的方法是将资源放在不同的层面:

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

答案 1 :(得分:0)

我会删除BasedOn属性 - 没有必要。考虑到这一点,合并Royale主题将应用按钮主题,你只想改变边距 - 样式本质上是加性的,所以它将结合Royale主题你自己的按钮主题没有指定BasedOn属性 - 这有意义吗?

干杯!

答案 2 :(得分:0)

请参阅this questionmy answer以获取另一个不需要您在每个窗口中指定资源字典的解决方案,并允许您动态解析BasedOn样式。