WPF:换肤
看了一篇博客,觉得样式很好看,就自己动手做了一下,做个总结。
效果:

选择不同的图片背景就会改变:

直接上代码:

每个Theme对应一张图,除了图的名称不同之外,Theme?.xaml中的内容相同:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ImageBrush x:Key="WindowBack" ImageSource="../Images/Theme1.jpg" Stretch="UniformToFill"/> </ResourceDictionary>
Theme1.xaml
窗口的背景为动态资源,且有一个默认值在App.xaml中(<ResourceDictionary Source="Themes/Theme3.xaml"/>):
<Window x:Class="ChangeTheme.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="800" Width="800" WindowStyle="None" WindowState="Maximized"
Background="{DynamicResource WindowBack}">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="ScrollViewerStyle1" TargetType="{x:Type ScrollViewer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid>
<Path Data="M101,0.5 L112,11.5 194.5,11.5 C197.26142,11.500001 199.5,13.738577 199.5,16.500001 L199.5,155.5 C199.5,158.26143 197.26142,160.5 194.5,160.5 L5.5,160.5 C2.7385788,160.5 0.5,158.26143 0.5,155.5 L0.5,16.500001 C0.5,13.738577 2.7385788,11.500001 5.5,11.5 L89.999999,11.5 z"
Fill="White" Stretch="Fill" Stroke="SeaGreen" StrokeThickness="0.5">
<!--B27A7A7A-->
<Path.Effect>
<DropShadowEffect Opacity="0.7" ShadowDepth="2" Direction="310"/>
</Path.Effect>
</Path>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid Height="320" Width="400" HorizontalAlignment="Right" VerticalAlignment="Top"> <ScrollViewer Width="350" Style="{DynamicResource ScrollViewerStyle1}" >
<WrapPanel Margin="15,50" x:Name="ImagePanel">
</WrapPanel>
</ScrollViewer>
</Grid>
</Window>
MainWindow.xaml
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadBackgroundImages();
} private void LoadBackgroundImages()
{
string path = @"../../Images";
var images =Directory.GetFiles(System.IO.Path.Combine(path)) ;
if (images.Length == ) return; foreach (var img in images)
{
Image imgTheme = new Image()
{
Width = ,
Stretch = Stretch.Fill,
Height = ,
Margin = new Thickness()
};
imgTheme.Name = System.IO.Path.GetFileNameWithoutExtension(img);
imgTheme.Tag = img;
imgTheme.Source = new BitmapImage(new Uri(img, UriKind.Relative));
imgTheme.MouseLeftButtonDown += new MouseButtonEventHandler(imgTheme_MouseLeftButtonDown);
ImagePanel.Children.Add(imgTheme);
}
} private void imgTheme_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Image image = sender as Image;
if (image != null && image.Source != null)
{
string imgName = image.Name;
ResourceHelper.LoadResource("pack://application:,,,/ChangeTheme;component/Themes/" + imgName + ".xaml");
} }
}
MainWindow.cs
static class ResourceHelper
{
public static void LoadResource(string fileName)
{
try
{
Application.Current.Resources.MergedDictionaries[] = new ResourceDictionary()
{
Source = new Uri(fileName, UriKind.RelativeOrAbsolute)
};
}
catch (Exception ex)
{
return;
}
}
}
ResourceHelper.cs
WPF:换肤的更多相关文章
- WPF换肤之八:创建3D浏览效果
原文:WPF换肤之八:创建3D浏览效果 上节中,我们展示了WPF中的异步以及界面线程交互的方式,使得应用程序的显示更加的流畅.这节我们主要讲解如何设计一个具有3D浏览效果的天气信息浏览器. 效果显示 ...
- WPF换肤之六:酷炫的时区浏览小精灵
原文:WPF换肤之六:酷炫的时区浏览小精灵 由于工作需要,经常要查看到不同地区的 当前时间,以前总是对照着时区表来进行加减运算,现在有了这个小工具以后,感觉省心了不少.下面是软件的截图: 效果图赏析 ...
- WPF换肤之七:异步
原文:WPF换肤之七:异步 在WinForm时代,相信大家都遇到过这种情形,如果在程序设计过程中遇到了耗时的操作,不使用异步会导致程序假死.当然,在WPF中,这种情况也是存在的,所以我们就需要寻找一种 ...
- WPF换肤之四:界面设计和代码设计分离
原文:WPF换肤之四:界面设计和代码设计分离 说起WPF来,除了总所周知的图形处理核心的变化外,和Winform比起来,还有一个巨大的变革,那就是真正意义上做到了界面设计和代码设计的分离.这样可以让美 ...
- WPF换肤之五:创建漂亮的窗体
原文:WPF换肤之五:创建漂亮的窗体 换肤效果 经过了前面四章的讲解,我们终于知道了如何拖拉窗体使之改变大小,也知道了如何处理鼠标事件,同时,也知道了如何利用更好的编写方式来编写一个方便实用和维护的换 ...
- WPF换肤之三:WPF中的WndProc
原文:WPF换肤之三:WPF中的WndProc 在上篇文章中,我有提到过WndProc中可以处理所有经过窗体的事件,但是没有具体的来说怎么可以处理的. 其实,在WPF中,要想利用WndProc来处理所 ...
- WPF换肤之二:可拉动的窗体
原文:WPF换肤之二:可拉动的窗体 让我们接着上一章: WPF换肤之一:创建圆角窗体 来继续. 在这一章,我主要是实现对圆角窗体的拖动,改变大小功能. 拖动自绘窗体的步骤 首先,通过上节的设计,我们知 ...
- WPF换肤之一:创建圆角窗体
原文:WPF换肤之一:创建圆角窗体 我们都期望自己的软件能够有一套看上去很吸引人眼球的外衣,使得别人看上去既专业又有美感.这个系列就带领着大家一步一步的讲解如何设计出一套自己的WPF的窗体皮肤,如果文 ...
- 有点激动,WPF换肤搞定了!
一如既往没废话! wpf桌面应用开发都是window内引入很多个UserControl. 如果你有通过不同颜色来换肤的需求,那么下面我就将整个过程! 分2个步骤: 1.主窗体背景色替换: 2.同时界面 ...
随机推荐
- 如何保证App外包的最终质量,不延期不烂尾?
选择App外包服务的客户,最害怕的就是App项目延期甚至烂尾.投入了巨大的时间和财富,结果最后App无法上线. 解决这个问题有两个方法:第一,在选择公司前,先了解清楚有关App外包的一切问题,做到心里 ...
- DuiLib学习笔记4——布局
有了前面三篇的基础,现在可以开始布局了. 首先任何布局都必须包含在<Window></Window>标签内,跟<html></html>很像. DuiL ...
- stl中的push_back
v_data.push_back(pdata);这句只是把指针pdata拷贝到 vector当中的一个指针p1当中 注意是拷贝也就是说当前pdata和p1指向同一个东西,p1在vector中.并不是将 ...
- Hdu 1443 Joseph
Joseph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 【001:ubuntu下搭建ESP8266开发环境--编辑 编译 下载】
系统环境:ubuntu 16.04 TLS 64BIT 编辑器: Eclipse CDT 版本 编译器:xtensa-lx106-elf 交叉编译工具链 下载工具:esptool.py pyseria ...
- jacob 多个web项目报错 jacob-1.14.3-x64.dll already loaded in another classloader jacob
多个web项目报错 jacob-1.14.3-x64.dll already loaded in another classloader jacob 这个问题困扰了很久,网上很多解决方案,很多都不成功 ...
- Python 时间和日期模块的常用例子
获取当前时间的两种方法 import datetime,time now = time.strftime("%Y-%m-%d %H:%M:%S") print now now = ...
- SQL2005中的事务与锁定(九)- 转载
------------------------------------------------------------------------ -- Author : HappyFlyStone - ...
- 三联运算&&字节码转换
三联运算 if 1 == 1: name = 'alex'else: name = 'sb' name = 'alex' if 1 == 1 else 'sb lambda f2 = lambda a ...
- pyenv ipython jupyter
pyenv pyenv 依赖安装 yum -y install git gcc make patch zlib-devel gdbm-devel openssl-devel sqlite-devel ...