Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF style causing loop
    primarykey
    data
    text
    <p>Why does the following XAML cause a stack overflow exception with some themes?</p> <pre><code>&lt;Application.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="ExpressionLight.xaml"/&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;Style x:Key="BaseButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"&gt; &lt;Setter Property="Margin" Value="5"/&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}"/&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; </code></pre> <p>I have tried several themes found on internet and about half of them causes the exception.</p> <p>Is there another way to apply a named <code>Style</code> as default?</p> <p><strong>Edit:</strong></p> <p>The problem is that the <code>Theme</code> adds the default style to the resource dictionary (an entry with the name <code>System.Windows.Control.Button</code>). Since a dictionary can only contain a single entry for each key it is not possible add a new default within the same resource dictionary.</p> <p>Don't know why it leads to a "stackoverflow" instead of a "duplicate key" exception. It is probably because of the special handling of merged dictionaries which can contain duplicate keys.</p> <p>The solution is to apply the named <code>Style</code> as default in code:</p> <pre><code>void AppStartup(object sender, StartupEventArgs args) { this.Resources[typeof(Button)] = this.Resources["BaseButtonStyle"]; .... } </code></pre> <p>Since the <code>BaseButtonStyle</code> is staticly bound to the theme it is of course not possible to change theme at runtime with this solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload