I have a ResourceDictionary named MyButton.xaml, in which the Style for x:Type ButtonBase is defined.
I know how to use this ResourceDictionary in order to define a Style in XAML.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://Application:,,,/Theme;component/MyButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type ButtonBase}}"/>
</ResourceDictionary>
</Window.Resources>
I'd like to do the same thing in the code-behind. Now I can write
var source = new Uri("Theme;component/MyButton.xaml", UriKind.Relative);
Resources.MergedDictionaries.Add(new ResourceDictionary {Source = source});
var buttonBaseStyle = TryFindResource(typeof (ButtonBase)) as Style;
However, I don't understand how to apply this buttonBaseStyle
to all Buttons (i.e. use it as the default style for Buttons) in the Window.
Could anyone tell me how?
Resources.Add("buttonStyle", s);
? If the key is duplicate, this can cause an Argument Exception.