Why does the following XAML cause a stack overflow exception with some themes?
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ExpressionLight.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="BaseButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}"/>
</ResourceDictionary>
</Application.Resources>
I have tried several themes found on internet and about half of them causes the exception.
Is there another way to apply a named style as default?
Edit:
The problem is that the Theme adds the default style to the resource dictionary (an entry with the name "System.Windows.Control.Button). 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.
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.
The solution is to apply the named style as default in code
void AppStartup(object sender, StartupEventArgs args) {
this.Resources[typeof(Button)] = this.Resources["BaseButtonStyle"];
....
}
Since the BaseButtonStyle is staticly bound to the theme it is of course not possible to change theme at runtime with this solution.