总结一下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文件的引用的更多相关文章

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

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

  2. WPF 中style文件的引用

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

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

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

  4. c# WPF SVG 文件的引用(SharpVectors)

    原文:c# WPF SVG 文件的引用(SharpVectors) 阿里巴巴矢量图标库提供了大量的 SVG 图标:https://www.iconfont.cn/ 但是 WPF 本身不支持 SVG 格 ...

  5. WPF定义样式文件的方式

    场景:一个页面中有两类按钮,分别为样式A和样式B,但是WPF中不能像Web一样定义多个样式 样式定义方法: 1. 一个一个写内联样式 2. 定义样式<style TargetType=" ...

  6. style文件的指定

    新建资源文件   写资源文件 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/prese ...

  7. JavaScript 将多个引用(样式或者脚本)放入一个文件进行引用

    1.将样式放入一个文件进行引用 @import url("../media/css/bootstrap.min.css"); @import url("../media/ ...

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

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

  9. WPF的Style的TargetType不同写法的异同

    原文:WPF的Style的TargetType不同写法的异同 <Style TargetType="TextBlock"> <Setter Property=&q ...

随机推荐

  1. 何时使用hadoop fs、hadoop dfs与hdfs dfs命令

    hadoop fs:使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs:只能操作HDFS文件系统相关(包括与Local FS间的操作),前者已经Deprecated,一般使用后 ...

  2. ADT-bundle

    eclipse 弹出 Version 1.4.2_03 of the JVM not suitable for this product.Version1.6or geeater is requir ...

  3. hdu 5311 Hidden String (BestCoder 1st Anniversary ($))(深搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=5311 Hidden String Time Limit: 2000/1000 MS (Java/Others)  ...

  4. Leetcode226:Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 /** * Definition for a ...

  5. find命令之exec

    find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec  参数后面跟的是command ...

  6. [置顶] 如何在浏览器中打开PDF文件并实现预览的思路与代码

    编写项目遇到一个需要在浏览器中打开PDF文件的问题.最终实现效果如下: 其实也就是简单的在浏览器中实现一个打开pdf文件,并有类似预览功能的边框. 其实在网上经常见到类似的页面,在浏览器中打开pdf文 ...

  7. MAT(3)获取dump文件

    方式一:添加启动参数 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=E:\Java\dump 生成的文件例如:java_pid2080.hprof ...

  8. How Tomcat Works(十八)

    在前面的文章中,如果我们要启动tomcat容器,我们需要使用Bootstrap类来实例化连接器.servlet容器.Wrapper实例和其他组件,然后调用各个对象的set方法将它们关联起来:这种配置应 ...

  9. ActionBar ShareActionProvider

    添加share按钮 添加share按钮的主要步骤: 1. 在ActionBar中添加share按钮 2. 从item中获取ShareActionProvider ShareActionProvider ...

  10. 笔记本键盘上没有break键的解决方案

    django在Windows上调试需要用ctrl+break终止服务器……笔记本键盘上没有break好尴尬…… 在百度搜了很多都没有找到,最后终于在谷歌上找到了英文版的解决方案. starting o ...