WPF的XAML 资源申明 类似HTML。

整体来说分3种

1.行类资源样式属性

1.1 行内属性

<Button Content="按钮" Foreground="White" FontSize="30"></Button>

1.2 行内样式 模板(没多大意义)

<Button Content="按钮">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid></Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid></Grid>
</ControlTemplate>
</Button.Template>
</Button>

2.头部资源样式属性

2.1 头部样式模板 (申明)

<Window.Resources>
<Style x:Key="xxx" TargetType="Button">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid></Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="xx" TargetType="Button">
<Grid></Grid>
</ControlTemplate>
</Window.Resources>

2.2 头部样式模板 (引用)

<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

  

3.全局资源样式属性

App.xaml

    <Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="xxx" TargetType="Button">
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid></Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="xx" TargetType="Button">
<Grid></Grid>
</ControlTemplate>
</ResourceDictionary>
</Application.Resources>

!!!!!!!!!!!!!!配合资源字典的使用!!!!!!!!!  

说明:

1.行类属性尽量少用,只有特殊控件 需要用到行内属性,

正确的做法是封装统一风格的所有控件。
  (例如按钮,统一高宽,字体,字体大小,然后申明到独立的资源字典中,
    在App.xaml中引用)

2.头部资源引用情况用于 不同 Window 适应不同主题或者风格的情况。

比如为某一个窗口申明一个当前窗口单独使用的样式。

(例如播放器的旋转控件,只有一个页面用到,只需要在Window级引用对应资源字典)

不放在App.xaml原因是为了降低内存消耗。

3.App.xaml 里面的资源引用适用于全局资源。理论上每一个被申明的Window
   都会创建一个对应资源字典的实例。除非是每个Window都会用到的模块,
    不然建议放到对应Window级

WPF XAML 资源样式模板属性存放位置的更多相关文章

  1. WPF xaml中列表依赖属性的定义

    列表内容属性 如上图,是一个列表标题排序控件,我们需要定义一个标题列表,从而让调用方可以自由的设置标题信息. 在自定义控件时,会遇到列表依赖属性,那么该如何定义呢? 下面是错误的定义方式: /// & ...

  2. WPF默认控件模板的获取和资源词典的使用

    一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1.创建 ...

  3. 使用StaticResource给控件定义公共的样式和属性来写界面XAML

    一:效果图 二:定义公共的样式和属性 在MainPage.xaml中 <phone:PhoneApplicationPage.Resources> <SolidColorBrush ...

  4. WPF自定义控件(二)の重写原生控件样式模板

    话外篇: 要写一个圆形控件,用Clip,重写模板,去除样式引用圆形图片可以有这三种方式. 开发过程中,我们有时候用WPF原生的控件就能实现自己的需求,但是样式.风格并不能满足我们的需求,那么我们该怎么 ...

  5. wpf提示背景,资源样式

    查找资源时多用element.TryFindResource() <TextBox FontSize="17" Height="26" Margin=&q ...

  6. WPF ResourceDictionary XAML资源 c#代码 获取与遍历

    使用C#代码来获取XAML资源,除去正常的FindResource.而且是能查询到资源的对象. 说实话还是很麻烦的. 比如说我现在有一堆静态资源放在xaml的资源中,我想通过绑定的方式来获取. 好比是 ...

  7. WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享

    系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与样式(3)-TextBox & Ric ...

  8. WPF自定义控件与样式(15)-终结篇

    原文:WPF自定义控件与样式(15)-终结篇 系列文章目录  WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与 ...

  9. [No000012F]WPF(7/7) - 样式,触发器和动画

    WPF Tutorial : Beginning [^] WPF Tutorial : Layout-Panels-Containers & Layout Transformation [^] ...

随机推荐

  1. IntelliJ IDEA 配置 smartGit

    教你如何在IntelliJ IDEA中配置smartGit? 一.第一种方式: 1.在启动IDEA工具时,点击下拉按钮"Check out from Version Control" ...

  2. CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"错误

    CXF 的 wsdl2java.bat 生产的代码,拷贝到目录,进行调研 web service接口时,抛出错误: Exception in thread "main" javax ...

  3. HDU 1874 畅通工程续(模板题——Floyd算法)

    题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...

  4. URL编码的方法

    Global 对象的encodeURI()和encodeURIComponent()方法可以对URI(Uniform ResourceIdentifiers,通用资源标识符)进行编码,以便发送给浏览器 ...

  5. pip install在Windows下报错解决

    报错: Traceback (most recent call last): File, in<module> load_entry_point('pip==1.4.1','console ...

  6. Hystrix-request cache(请求缓存)

    开启请求缓存 请求缓存在run()和construce()执行之前生效,所以可以有效减少不必要的线程开销.你可以通过实现getCachekey()方法来开启请求缓存. package org.hope ...

  7. ThinkServer TD340服务器安装操作系统[转]

    一:服务器概况 服务器是联想旗下ThinkServer品牌TD340型号服务器,服务器标配32G内存,1T硬盘.其中服务器使用RAID(磁盘阵列)技术,拥有一个RAID卡,服务器标配一个大小为1T的磁 ...

  8. Linux 查找命令汇总

    linux下查找命令挺多,本文以列表方式说明which.whereis.locate.find命令的区别: 命令 概述 语法 示例 适用OS 搜索结果 which 在PATH变量指定的路径中,搜索某个 ...

  9. java.lang.IllegalArgumentException: Document base D:\Project\workspace\space\.metadata\.plugins\org.eclipse.wst.server.core\tmp7\wtpwebapps\Blog

    java.lang.IllegalArgumentException: Document base D:\Project\workspace\space\.metadata\.plugins\org. ...

  10. Servlet--SingleThreadModel接口,RequestDispatcher接口

    SingleThreadModel接口 定义 public interface SingleThreadModel; 这是一个空接口,它指定了系统如何处理对同一个 Servlet 的调用.如果一个 S ...