原文: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. PHP数组foreach循环如何实现逆序访问?

    PHP数组foreach循环如何实现逆序访问? 一.总结 1.array_reverse($array) :foreach(array_reverse($array) as $key=>$val ...

  2. 小强的HTML5移动开发之路(45)——汇率计算器【1】

    这两天看了<PhoneGap实战>上面有一个汇率计算器的例子,个人觉得比较好,就拿出来和大家分享一下,在接下来的几篇文章中我们来一起完成这个PhoneGap + Jquery mobile ...

  3. 关于用WebView或手机浏览器打开连接问题

    1.通常情况下 大家可能都想使用WebView打开网页内部链接而不想再调用手机浏览器,我们可以通过以下两种方法实现: (1)为WebView设置一个WebViewClient,并重写shouldOve ...

  4. TCP协议的一些认识及实践

    http://www.2cto.com/net/201210/163047.html 一.简介 引用<TCP/IP详解-卷1>中的介绍,TCP与UDP使用相同的网络层(IP层),TCP却向 ...

  5. WPF实现射线效果动画

    原文:WPF实现射线效果动画 最近的一个项目中有个需求是:从一个点向其它多个点发出射线,要求这些射线同时发出,同时到达. 我就想到了用WPF的动画来实现.WPF中有Line类用于绘制直线,但这个类中好 ...

  6. Browser security standards via access control

    A computing system is operable to contain a security module within an operating system. This securit ...

  7. 单点登录原理与简单实现--good

    一.单系统登录机制 1.http无状态协议 web应用采用browser/server架构,http作为通信协议.http是无状态协议,浏览器的每一次请求,服务器会独立处理,不与之前或之后的请求产生关 ...

  8. Druid 专题

    数据源配置: #datasource #Introductions: https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7 ...

  9. Fast exit from dram self-refresh

    Embodiments of the invention describe a dynamic random access memory (DRAM) device that may abort a ...

  10. altium designer电气符号和包的常用元素

    1. 标准电阻:RES1.RES2.包裹:AXIAL-0.3至AXIAL-1.0 两port可变电阻器:RES3.RES4.包裹:AXIAL-0.3至AXIAL-1.0 三port可变电阻:RESIS ...