This is an extension to my previous answered question
How to Change Theme XAML with ComboBox?
I have a ComboBox
that changes Themes between ThemeBlue.xaml
and ThemeRed.xaml
.
Here's the example project file:
https://drive.google.com/open?id=0BycnCTAQ1U7gSU5kUUdaNzRIZDg
ThemeBlue.xaml: https://kopy.io/8HcDd
ThemeRed.xaml: https://kopy.io/iWWLC
Blue Theme
Red Theme
Problem:
When you choose Red, only the Window Background color is changed, but not the TextBox
or Button
.
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#
private void cboTheme_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string theme = cboTheme.SelectedItem.ToString();
App.Current.Resources.MergedDictionaries.Clear();
App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Theme" + theme + ".xaml", UriKind.RelativeOrAbsolute) });
// Save Theme for next launch
Settings.Default["Theme"] = cboTheme.SelectedItem.ToString();
Settings.Default.Save();
}
App.xaml
The theme file is loaded through App.xaml
at startup
<Application x:Class="MultiTheme.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultiTheme"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ThemeBlue.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</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" />
Use the
DynamicResource
markup extension instead ofStaticResource
when you set theStyle
properties inMainWindow.xaml
:What's the difference between StaticResource and DynamicResource in WPF?