WPF 之 style文件的引用
总结一下WPF中Style样式的引用方法。
一、内联样式:
直接设置控件的Height、Width、Foreground、HorizontalAlignment、VerticalAlignment等属性。
以设置一个Botton控件的样式为例,如:
<Button Content="Button" Name="btnDemo"
Height="72" Width="150" Foreground="White" Background="Blue" HorizontalAlignment="Left"
VerticalAlignment="Top" Margin="170,132,0,0" />
这种方式比较简单,但是代码不能复用。
二、嵌入样式:
在页面<Window.Resources>节点下添加样式,然后在需要的控件上设置Style属性。还是以Botton控件为例。
1、在页面<Window.Resources>节点下添加一个Key值叫“myBtnStyle”的样式
<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、 设置Botton控件的Style属性为"{StaticResource BtnStyle}"
<Button Content="Button" Name="btnDemo" Style="{StaticResource BtnStyle}"/>
TargetType="{x:Type Button}"指定了该样式适用于Botton类型的控件,Key="myBtnStyle"如果不设置该值,则该样式将适用于所有的Botton控件,而设置了其值为“myBtnStyle”,则只用于设置了 Style="{StaticResource myBtnStyle}"的Botton控件。这就好比CSS中的元素选择器和类选择器。
这种方式可以使得单个页面上的控件能够复用一个样式,比第一种方式面向对象了一步。
三、外联样式:
1、新建一个.xaml资源文件,如/Theme/Style.xaml
2、 在Style.xaml文件里编写样式代码
Style.xaml:
<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文件的<Application.Resources>
或者普通页面的<Window.Resources>
或者用户控件的 <UserControl.Resources> 节点下
添加相应的ResourceDictionary,配置引用Style.xaml:
app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/应用名称;component/Theme/Style.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
或者MainWindow.xaml:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Theme/BtnStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<ResourceDictionary.MergedDictionaries>节点下可以添加多个资源文件
这种方式相比前面两种使得样式和结构又更进一步分离了。
在App.xaml引用,是全局的,可以使得一个样式可以在整个应用程序中能够复用。在普通页面中引用只能在当前页面上得到复用。
4、设置Botton控件的Style属性为"{StaticResource myBtnStyle}" 和上面的一样。
四、用C#代码动态加载资源文件并设置样式
1、新建资源文件:同上面的1,2两步。
2、在后台编写代码
首先,将我们自定义的样式加载到应用程序的资源字典中。
ResourceDictionary resourceDictionary =newResourceDictionary();
Application.LoadComponent(resourceDictionary, new Uri("/PhoneApp1;component/Resources/BtnStyle.xaml", UriKind.Relative));
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
其次,为控件添加样式。
this.btnDemo.SetValue(Button.StyleProperty, Application.Current.Resources["BtnStyle"]);
WPF 之 style文件的引用的更多相关文章
- WPF中Style文件的引用——使用xaml代码或者C#代码动态加载
原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...
- WPF 中style文件的引用
原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment. ...
- WPF中Style文件引用另一个Style文件中的样式
第1种方法: 直接在当前Style文件(*.xaml)文件中使用: <ResourceDictionary.MergedDictionaries>来进行合并 <!-- 关键是注意so ...
- c# WPF SVG 文件的引用(SharpVectors)
原文:c# WPF SVG 文件的引用(SharpVectors) 阿里巴巴矢量图标库提供了大量的 SVG 图标:https://www.iconfont.cn/ 但是 WPF 本身不支持 SVG 格 ...
- WPF定义样式文件的方式
场景:一个页面中有两类按钮,分别为样式A和样式B,但是WPF中不能像Web一样定义多个样式 样式定义方法: 1. 一个一个写内联样式 2. 定义样式<style TargetType=" ...
- style文件的指定
新建资源文件 写资源文件 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese ...
- JavaScript 将多个引用(样式或者脚本)放入一个文件进行引用
1.将样式放入一个文件进行引用 @import url("../media/css/bootstrap.min.css"); @import url("../media/ ...
- WPF中使用文件浏览对话框的几种方式
原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...
- WPF的Style的TargetType不同写法的异同
原文:WPF的Style的TargetType不同写法的异同 <Style TargetType="TextBlock"> <Setter Property=&q ...
随机推荐
- JavaScript 变量、作用域及内存详解
基本类型值有:undefined,NUll,Boolean,Number和String,这些类型分别在内存中占有固定的大小空间,他们的值保存在栈空间,我们通过按值来访问的. (1)值类型:数值.布尔值 ...
- 【原】Hadoop伪分布模式的安装
Hadoop伪分布模式的安装 [环境参数] (1)Host OS:Win7 64bit (2)IDE:Eclipse Version: Luna Service Release 2 (4.4.2) ( ...
- iOS 中UIButton的 settitle 和 titlelabel的使用误区
UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void) ...
- C# Devexpress学习--LabelControl
A LabelControl can display an image (regular or animated GIF file). Different images can be provided ...
- Objective-C 学习记录--toches、Motion/Size/Rect/Point/CGFloat/protocol
- (void)touchesBegan touchesEnd touchesCancelled touchesMoved //代表的是手指在屏幕上的动作,开始 结束 取消 移动 //还有就是代表摇动 ...
- Oracle 将不同列的值拼接成一个 字符串
利用拼接操作符“||”或者 CONCAT('','')函数,将不同列的值 拼接成一个 字符串 -- 方法一:推荐 SELECT S.TEAM ||'**'|| S.NAME ||'**'|| S. ...
- GetSafeHwnd()函数解释[转]
当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数,通过下面的例子来看其理由: CWnd *pwnd = FindWindow(“ ...
- C++静态成员函数小结 [转]
类中的静态成员真是个让人爱恨交加的特性.我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不在被动. 静态类成员包括静态数据成员和静态函数成员两部分. 一 静态数据成员: 类体中 ...
- 使用#pragma阻止一些warnings
这篇博客的内容都是记的网上的.是流水账.只是记录下来以便日后之有,避免每次重新google. #pragma除了可以用来把不同功能的代码进行分隔组织外还可以用来disable一些warnings.这在 ...
- 闲话Cache:始篇
Caching(缓存)在现代的计算机系统中是一项最古老最基本的技术.它存在于计算机各种硬件和软件系统中,比如各种CPU, 存储系统(IBM ESS, EMC Symmetrix…),数据库,Web服务 ...