4

除了 Royale 主题样式,我希望每个按钮都有 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
}

没有例外细节,因为:

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

4

3 回答 3

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>
于 2009-10-09T09:32:21.270 回答
0

我会删除 BasedOn 属性 - 这不是必需的。这样想,合并 Royale 主题将应用按钮主题,而您只想更改边距 - 样式本质上是相加的,因此它将结合 Royale 主题您自己的按钮主题而不指定 BasedOn 属性 - 确实有道理?

干杯!

于 2009-05-29T18:02:20.460 回答
0

请参阅此问题对另一个解决方案的回答,该解决方案不需要您在每个窗口中指定资源字典并允许您动态解析 BasedOn 样式。

于 2010-06-23T07:26:14.053 回答