WPF 中style文件的引用
总结一下WPF中Style样式的引用方法:
一,内联样式:
直接设置控件的Height、Width、Foreground、HorizontalAlignment、VerticalAlignment等属性。以设置一个Botton控件的样式为例,如:
复制代码
<Grid x:Name="ContentPanel" >
<Button Content="Button" Name="btnDemo"
Height="72"
Width="150"
Foreground="White"
Background="Blue"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="170,132,0,0"
Grid.Row="1" />
</Grid>
这种方式比较简单,但是代码不能复用。
二,嵌入样式:
在页面<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}"
<Grid x:Name="ContentPanel" >
<Button Content="Button" Name="btnDemo"
Style="{StaticResource BtnStyle}"/>
</Grid>
解释一下,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 Source="Resources/BtnStyle2.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样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...
- WPF中Style文件引用另一个Style文件中的样式
第1种方法: 直接在当前Style文件(*.xaml)文件中使用: <ResourceDictionary.MergedDictionaries>来进行合并 <!-- 关键是注意so ...
- WPF中使用文件浏览对话框的几种方式
原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...
- 在WPF中使用文件夹选择对话框
开发中有时会想实现"选择某个文件夹"的效果: 在WPF中,使用Microsoft.Win32.OpenFileDialog只能选择文件,FolderBrowserDialog只能用 ...
- WPF中TextBox文件拖放问题
在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...
- 解决WPF中TextBox文件拖放问题
在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...
- WPF中选择文件和选择文件夹的方法
最近从winform转WPF,遇到了各种各样的问题.然而网上的关于WPF的资料少之又少,甚至连基本的文件选择操作,百度搜索的首页都没有一个比较好的方法.所以,踩了几个坑之后,我把我得到的方法分享给大家 ...
- VS2012添加对DirectX SDK中需要文件的引用
error LNK2019: 无法解析的外部符号 _DirectDrawCreateEx@16,该符号在函数 "int __cdecl DD_Init(int,int,int)" ...
随机推荐
- An Overview of Cisco IOS Versions and Naming
An Overview of Cisco IOS Versions and Naming http://www.ciscopress.com/articles/article.asp?p=210654 ...
- [Angular] Observable.catch error handling in Angular
import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/opera ...
- Mysql存储过程中使用cursor
一.表 学生表 CREATE TABLE `t_student` ( `stuNum` int(11) NOT NULL auto_increment, `stuName` varchar ...
- [Django] Get started with Django -- Install python and virtualenv
Install python3 on MacOS: brew install python3 Come alone with python3, there are also some other to ...
- 【19.27%】【codeforces 618D】Hamiltonian Spanning Tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Windows PowerShell 学习之——Cmdlet处理生命周期
这一次介绍一下Cmdlet处理过程的生命周期 总共分为六个部分 1.概述 2. 命令行输入绑定参数(parameters) 3. 开始指令处理 4. 接受管道输入绑定参数 5. 处理记录 6. 处理记 ...
- 51系列小型操作系统精髓 简单实现6 C语言版待改进
#include "STC12C5A.H" #define TIMER_RELOAD() {TL0=0x00;TH0=0xC4;}//使能T/C 初始10ms #define ...
- 转载来自朱小厮的博客的NIO相关基础篇
用户空间以及内核空间概念 我们知道现在操作系统都是采用虚拟存储器,那么对32位操作系统而言,它的寻址空间(虚拟存储空间)为4G(2的32次方).操心系统的核心是内核,独立于普通的应用程序,可以访问受保 ...
- Notice: Undefined index: user in D:\phpStudy\WWW\js\ls\lsmc\php\add.php on line 9
原文:Notice: Undefined index: user in D:\phpStudy\WWW\js\ls\lsmc\php\add.php on line 9 (初用数据库(mysql)做用 ...
- c语言学习笔记(11)——枚举
# include <stdio.h> enum WeekDay //定义了一个数据类型(值只能写一下值) { MonDay, TuesDay, WednesDay, ThursDay, ...