WPF换肤
style1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="BtnStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Blue"/>
</Style>
</ResourceDictionary>
style2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="BtnStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Yellow"/>
</Style>
</ResourceDictionary>
前台:
<Window x:Class="WpfMarquee.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window5" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfMarquee;Component/style1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid >
<Button Name="Button1" Style="{StaticResource BtnStyle}" />
<Button Name="Button2" Click="button2_Click" />
</Grid>
</Window>
后台:
private void button2_Click(object sender, RoutedEventArgs e)
{
string packUri = @"/WpfMarquee;Component/style2.xaml";
ResourceDictionary myResourceDictionary = Application.LoadComponent(new Uri(packUri, UriKind.Relative)) as ResourceDictionary;
this.Resources.MergedDictionaries.Add(myResourceDictionary);
}
问题:Button1变不了黄色!