tags:

views:

44

answers:

1

Is there a way to replace all the brush definitions for a WPF application at runtime, and only declare the styles that use it once? This would be useful for different color schemes, but keep the same UI and declare it once. All the examples I can find duplicate the styles in the different theme files - is this the only way to do it?

Little example:

Blue.xaml

<SolidColorBrush x:Key="DefaultBackgroundBrush" Color="Blue"/>

Yellow.xaml

<SolidColorBrush x:Key="DefaultBackgroundBrush" Color="Yellow"/>

generic.xaml?

<Style TargetType="{x:Type Button}">
  <Setter Property="Background" Value="{StaticResource DefaultBackgroundBrush}" />
</Style>
A: 

Figured it out myself just after posting the question, often like that ;)

The code below was for testing, so don't mind the un-sexyness of it:

private void MenuItemBlue_Click(object sender, RoutedEventArgs e)
{
    ResourceDictionary genericSkin = new ResourceDictionary();
    genericSkin.Source = new Uri(@"/Themes/" + "generic" + ".xaml", UriKind.Relative);

    ResourceDictionary blueSkin = new ResourceDictionary();
    blueSkin.Source = new Uri(@"/Themes/" + "blue" + ".xaml", UriKind.Relative);

    Application.Current.Resources.MergedDictionaries.Clear();

    Application.Current.Resources.MergedDictionaries.Add(genericSkin);
    Application.Current.Resources.MergedDictionaries.Add(blueSkin);
}

And change the style defined in "generic.xaml" to DynamicResource

<Style TargetType="{x:Type Button}">
    <Setter Property="Background" Value="{DynamicResource defaultColor}" />
</Style>

Other suggestions are most welcome though.

Islandwind