WPF换肤

huangwenya123 2014-03-11 11:32:19
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变不了黄色!
...全文
156 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangwenya123 2014-03-13
  • 打赏
  • 举报
回复
问题解决了,Demo
huangwenya123 2014-03-11
  • 打赏
  • 举报
回复
引用 1 楼 KumaPower 的回复:
复制一份系统Button的默认模板,在模板里面改其背景的颜色吧。
我要的不仅仅是Button的样式,我是想动态引用资源文件,而这个资源文件里有许多控件的样式,可以一下把全部控件的样式动态改变,简单来说就是换肤。
huangwenya123 2014-03-11
  • 打赏
  • 举报
回复
引用 2 楼 hbu_pig 的回复:
首先直接把样式放到当前页面的资源中,看看button1能不能改变样式,若改变,才能保证你这个样式是写的正确的。 而后看下你的后台带式是否正确的添加到资源中。 最后Button1.UpdateLayout(),试试。
样式已经试了可以显示效果,再者我引用文件的原因是因为我要动态改变的不仅仅是Button控件的样式,还有其它控件,在线等,求Demo
huangwenya123 2014-03-11
  • 打赏
  • 举报
回复
引用 1 楼 KumaPower 的回复:
复制一份系统Button的默认模板,在模板里面改其背景的颜色吧。
求Demo,问题解决立刻给分,在线等
欢乐的小猪 2014-03-11
  • 打赏
  • 举报
回复
首先直接把样式放到当前页面的资源中,看看button1能不能改变样式,若改变,才能保证你这个样式是写的正确的。 而后看下你的后台带式是否正确的添加到资源中。 最后Button1.UpdateLayout(),试试。
Bonjour-你好 2014-03-11
  • 打赏
  • 举报
回复
复制一份系统Button的默认模板,在模板里面改其背景的颜色吧。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧