Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900