WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观和行为,如同CSS代码一般。
总结一下WPF中Style样式的引用方法:
一、内联样式
直接在控件的内部xaml代码中书写各种依赖属性,如下:
<Button Height="30" Width="60" Background="Green" Foreground="White">
</Button>
这种方式比较直接方便,适用于单个控件、代码量较少的Style设置,代码不能重用。
二、嵌入样式:
在窗体(Window)或者页面(Page)的资源节点下面(Window.Resources或者Page.Resources)添加Style代码,这样在整个窗体或者页面范围内可以实现Style代码重用。
第1步,书写Style代码:
<Window.Resources>
<Style x:Key="myBtnStyle" TargetType="{x:Type Button}">
<Setter Property="Height" Value="72" />
<Setter Property="Width" Value="150" />
<Setter Property="Foreground" Value="Red" />
<Setter Property="Background" Value="Black" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
</Window.Resources>
第2步,在Button的xaml代码中引用Style:
<Button Style="{StaticResource myBtnStyle}"></Button>
三、外联样式:
前面说的两种方式,都无法设置整个应用程式里面的全局Style,现在我们介绍全局设置Style的方式。
1.新建一个.xaml的资源文件,如/Theme/Style.xaml。
2.在该文件中编写样式代码:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<Style x:Key="myBtnStyle" TargetType="Button">
<Setter Property="Height" Value="72" />
<Setter Property="Width" Value="150" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Blue" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
</ResourceDictionary>
3.在App.xaml文件中的<
Applictaion.Resources>
节点下添加<
ResourceDictionary>
节点:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
这种方式相比前面两种使得样式和结构又更进一步分离了。在App.xaml引用,是全局的,可以使得一个样式可以在整个应用程序中能够复用。在普通页面中引用只能在当前页面上得到复用。
四、使用C#代码动态加载Style
1.假设应用程式用已经有了全局的资源文件(上面的方法中有介绍)。
2.编写C#代码:
Button btn =new Button();
btn.SetValue(Button.StyleProperty, Application.Current.Resources["资源名称"]);

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
C# WPF 中用代码模拟鼠标和键盘的操作
原文: C# WPF 中用代码模拟鼠标和键盘的操作 原文地址 C#开发者都知道,在Winform开发中,SendKeys类提供的方法是很实用的。但是可惜的是,在WPF中不能使用这个方法了。 我们知道,在WPF中非UI线程刷新UI线程,需要使用Dispatcher.Invoke((Action)delegate { /* Your code is put here */ });方法。这里调用System.Windows.Forms.SendKeys.Send()方法会报错。 下面这个代码文件做了一个很好的包装,可以下载后参考: Simulation.zip 如何使用呢? 很简单, 要敲一个键, 比如回车: Keyboard.Press(Key.Enter); Keyboard.Release(Key.Enter); 要敲一个组合键:比如Alt+F4: Keyboard.Press(Key.LeftAlt); Keyboard.Press(Key.F4); Keyboard.Release(Key.LeftAlt); Keyboard.Release(Key.F4); 要敲一段文字:...
-
下一篇
C# 获取utc时间,以及utc datetime 互相转化
原文: C# 获取utc时间,以及utc datetime 互相转化 C# 获取utc时间,以及utc datetime 互相转化 大部分源于http://blog.sina.com.cn/s/blog_4c6e822d0102dsdz.html 刚开始学习一点C#知识,研究一下UTC时间获取,如下 DateTime dt = DateTime.UtcNow; 另: DateTime.UtcNow.ToString()输出的是0时区的事件, DateTime.Now.ToString()输出的是当前时区的时间,我们中国使用的是东八区的时间,所以差8个小时 以下是互相转换class class utc { public static int ConvertDateTimeInt(System.DateTime time) { double intResult = 0; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); in...
相关文章
文章评论
共有0条评论来说两句吧...