原文: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"]);

各位大虾见笑了,呵呵!!!

最新文章

  1. Net设计模式实例之桥接模式( Bridge Pattern)
  2. [Architect] ABP(现代ASP.NET样板开发框架) 翻译
  3. dwr2反推
  4. Codeforces Round #154 (Div. 2) : B
  5. spring 入门笔记(一)
  6. P2P/WSN信任建模与仿真平台
  7. Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/springframework/boot/context/embedded/ServletRegistrationBean
  8. PE文件详解(六)
  9. HTTPS的原理解析
  10. Linux之 proc文件系统
  11. Swift学习之道
  12. Kerberos安全体系详解---Kerberos的简单实现
  13. Faster rcnn代码理解(3)
  14. SQL LOAD TABLE tbl_name FROM MASTER语法 把表的拷贝从主服务器转移到从属服务器。
  15. 【一】、搭建Hadoop环境----本地、伪分布式
  16. c++ 中map 的find 函数用法
  17. SharePoint 表单认证创建用户
  18. C#对两种类型动态库的使用
  19. [leetcode greedy]455. Assign Cookies
  20. 复习JavaScript随手记

热门文章

  1. Android图文具体解释属性动画
  2. 切换-5.7-传统复制切换成GTID复制
  3. [CortexM0--stm32f0308]Option Byte
  4. 【hdu 3389】Game
  5. [React Native] Installing and Linking Modules with Native Code in React Native
  6. 【t094】区间运算
  7. boost::any在降低模块之间耦合性的应用
  8. zookeeper 分布式安装/配置/启动
  9. Android菜鸟的成长笔记(21)——跨进程调用Service
  10. 【18.40%】【codeforces 631D】Messenger