How can I apply different color themes to my program using a ComboBox?
Such as ThemeBlue.xaml
, ThemeRed.xaml
, ThemePurple.xaml
.
I need it to swap out one theme xaml file for another on selection change.
Here's the example project file:
https://drive.google.com/open?id=0BycnCTAQ1U7gSU5kUUdaNzRIZDg
Themes
I have a theme file ThemeBlue.xaml
with colors set for controls and window.
<SolidColorBrush x:Key="TextBoxCustom.Static.Background" Color="Blue"/>
<SolidColorBrush x:Key="TextBoxCustom.Static.Border" Color="Blue"/>
<Style x:Key="TextBoxCustom" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="{StaticResource TextBoxCustom.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource TextBoxCustom.Static.Border}"/>
...
ComboBox Theme Select
XAML
<ComboBox x:Name="cboTheme" Style="{StaticResource ComboBoxCustom}" HorizontalAlignment="Left" Margin="199,120,0,0" VerticalAlignment="Top" Width="75" SelectionChanged="themeSelect_SelectionChanged">
<System:String>Blue</System:String>
<System:String>Red</System:String>
<System:String>Purple</System:String>
</ComboBox>
C#
This is where I need help to apply the theme file.
private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Apply theme file to App.xaml from option selected
// Blue
// Red
// Purple
}
App.xaml
The theme file is loaded through App.xaml at startup
<Application x:Class="MyProgram.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="ThemeBlue.xaml"/>
</Application.Resources>
</Application>
MainWindow.xaml
A TextBox with the Theme Style applied:
<TextBox x:Name="textBox1" Style="{StaticResource TextBoxCustom}" HorizontalAlignment="Left" Height="22" Margin="93,43,0,0" Width="464" />
Saving Theme
I save and load the theme through Settings.
private void themeSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Save Theme for next launch
Settings.Default["Theme"] = cboTheme.SelectedItem.ToString();
Settings.Default.Save();
Settings.Default.Reload();
}
You neet to replace the resource of the App.xaml with another resource in code behind. You can take the resource by loading the component. Here is a possible solution:
Try this:
App.xaml: