原文: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文件的引用的更多相关文章

  1. WPF中Style文件的引用——使用xaml代码或者C#代码动态加载

    原文:WPF中Style文件的引用--使用xaml代码或者C#代码动态加载 WPF中控件拥有很多依赖属性(Dependency Property),我们可以通过编写自定义Style文件来控制控件的外观 ...

  2. WPF 之 style文件的引用

    总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...

  3. WPF中Style文件引用另一个Style文件中的样式

    第1种方法: 直接在当前Style文件(*.xaml)文件中使用: <ResourceDictionary.MergedDictionaries>来进行合并 <!-- 关键是注意so ...

  4. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  5. 在WPF中使用文件夹选择对话框

    开发中有时会想实现"选择某个文件夹"的效果: 在WPF中,使用Microsoft.Win32.OpenFileDialog只能选择文件,FolderBrowserDialog只能用 ...

  6. WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  7. 解决WPF中TextBox文件拖放问题

    在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...

  8. WPF中选择文件和选择文件夹的方法

    最近从winform转WPF,遇到了各种各样的问题.然而网上的关于WPF的资料少之又少,甚至连基本的文件选择操作,百度搜索的首页都没有一个比较好的方法.所以,踩了几个坑之后,我把我得到的方法分享给大家 ...

  9. VS2012添加对DirectX SDK中需要文件的引用

    error LNK2019: 无法解析的外部符号 _DirectDrawCreateEx@16,该符号在函数 "int __cdecl DD_Init(int,int,int)" ...

随机推荐

  1. Android多媒体开发(3)————使用Android NKD编译havlenapetr-FFMpeg-7c27aa2

    1. 使用NDK去编译官方的FFmpeg原版的话,还得自己实现JNI层与Java层,工程量比较大.所以移植FFmpeg到Android平台时,可以移植一些已经实现JNI与JAVA层的开源项目,毕竟软件 ...

  2. [Angular] Working with FormArray

    'FormArray' can work with array of 'FormGroup' or 'FormControl'. form = new FormGroup({ stock: new F ...

  3. tcp注意点

    tcp注意点 tcp服务器一般情况下都需要绑定,否则客户端找不到这个服务器 tcp客户端一般不绑定,因为是主动链接服务器,所以只要确定好服务器的ip.port等信息就好,本地客户端可以随机 tcp服务 ...

  4. Framework for Graphics Animation and Compositing Operations

    FIELD OF THE DISCLOSURE The subject matter of the present disclosure relates to a framework for hand ...

  5. one hot 编码的实现

    one hot 编码,针对的是类别性属性(categorical),类别型属性可以为特征向量中的任一属性,比如性别(one hot 编码的意义在于,属性之间不具有数值上大小的区别,在对最后结果的影响上 ...

  6. hbase 2.0.2 put和delete的一些坑

    测试的inbox表为多版本表,封装的scanTable已设置查询全部版本,以下的测试基于hbase2.0.2 一.put(针对相同的rowkey) 测试1.使用方法链的形式对同一个put添加数据到不同 ...

  7. VB的MSHFlexGrid控件内容导入Excel

    机房收费系统中有非常多窗口用到导出到Excel,说一下vb与Excel的交互,怎样才干将MSHFlexgrid中的内容导出到Excel. 首先在VB中加入引用Microsoft Excel 14.0 ...

  8. XMPP之ios即时通讯客户端开发-配置XMPP基本信息之工程代码(五)

    登录功能完成以后包含以下代码文件: AppDelegate.h AppDelegate.m LoginViewController.h LoginViewController.m LoginUser. ...

  9. erlang浅谈

    优点: 1.面向并发,有成熟而且久经考验的框架可供使用,网络部分已经经过了良好封装 2.内存缓存解决方案进程字典,前者的读写速度是50NS-100Ns级别的 3.对二进制数据解析的语法是直观,简单,强 ...

  10. Display controller

    Field of the Invention The present invention relates to a display controller. Background to the inve ...