2009-05-29 18 views
4

Royaleのテーマスタイルに加えて、すべてのボタンに5ポイントの余白を設定します。WPF:テーマのスタイルを拡張する - StackOverflowException

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> 

それはコンパイルが、私は得る:

型 'System.StackOverflowException' の未処理の例外がPresentationFramework.dll

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

例外の詳細はありません:

{。現在のスレッドがスタックオーバーフロー状態にあるので、式を評価することはできません}

+0

例外がどこで発生したのかわかりますか? – ChrisF

答えて

5

これはあなたのスタイルとPresentationFramework.Royaleで定義されているスタイルとの間の循環参照のようです。 workaroudは異なるレベルでリソースを配置するために、次のようになります。

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

どのように循環参照を引き起こしていたのですか?そしてスコープを変更することで解決された理由を説明できますか? – teenup

0

私はBASEDON属性を削除するだろう - それは必要はありません。このように考えると、Royaleテーマをマージするとボタンテーマが適用され、マージンを変更したいだけです。スタイルは基本的には相補的なので、Royaleテーマを組み合わせて、BasedOn属性- それは理にかなっていますか?

乾杯!

+0

私は、近似スコープのスタイル*が以前のスタイルを上書きする、つまり最大で1つのスタイルを適用できるという印象を受けました。そして、BasedOn属性はスタイルを*拡張する方法です。 –

+0

ああ、あなたは絶対に正しいです。興味深い - 私はAeroのテーマを使ってあなたに与えた提案を試し、BasedOn = "..."を省略するとボタンのスタイルが元のXPテーマに戻されます。うわー:) あなたがソースの多くを共有することができれば、おそらく私は見てみることができます。 – Charles

0

すべてのウィンドウにリソースディクショナリを指定する必要がありますし、あなたが動的にBASEDONのスタイルを解決することができない別の解決策をthis questionmy answerを参照してください。

関連する問題