I have two styles in two resource dictionaries. When application load then I have loaded one (`Dictionary1.xaml`) resource. After that when I click on button then I want to change style. i.e I want to load `Dictionary2.xaml`. I have changed but there is no effect on my button.
Dictionary1.xaml
<resourcedictionary>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button" x:Key="s1">
<setter property="Background" value="Red">
<setter property="Foreground" value="Red">
</Style>
Dictionary2.xaml
<resourcedictionary>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button" x:Key="s1">
ResourceDictionary r = new ResourceDictionary();
r.Source = sUri;
Resources.MergedDictionaries.Add(r);
Apply style in page: Main.xaml
<grid x:name="LayoutRoot" removed="White" xmlns:x="#unknown">
<Button Style="{StaticResource s1}" Content="Button" HorizontalAlignment="Left" Margin="195,133,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
When click on button then:
private void Button_Click(object sender, RoutedEventArgs e)
{
Uri sUri = new Uri("/KeyCheck;component/Dictionary1.xaml", UriKind.Relative);
ResourceDictionary r = new ResourceDictionary();
r.Source = sUri;
App.Current.Resources.MergedDictionaries.Remove(r);
sUri = new Uri("/KeyCheck;component/Dictionary2.xaml", UriKind.Relative);
r = new ResourceDictionary();
r.Source = sUri;
App.Current.Resources.MergedDictionaries.Add(r);
}