amin uddin

amin uddin

  • NA
  • 23
  • 1k

Change key baised style in silverlight

Jan 30 2014 2:34 AM
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>
</ResourceDictionary>
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">
<Setter Property="Background" Value="Green</Setter>
<Setter Property="Foreground" Value="Green"></Setter>/>
</Style>
</ResourceDictionary>
When application load then
Uri sUri = new Uri("/KeyCheck;component/Dictionary1.xaml", UriKind.Relative);
ResourceDictionary r = new ResourceDictionary();
r.Source = sUri;
Resources.MergedDictionaries.Add(r);
Apply style in page: Main.xaml
<Grid x:Name="LayoutRoot" Background="White">
<Button Style="{StaticResource s1}" Content="Button" HorizontalAlignment="Left" Margin="195,133,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
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);
}

Answers (1)