一、获取默认的控件模板

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

1、创建一个Button

2、在界面上选择Button,右键->编辑模板->编辑副本 ,即可看到XAML中自动生成了原始的控件模板

3、可以在默认模板上修改其中的一些属性运行测试是否生效

这样在默认的控件模板上编辑,只修改需要修改的部分即可,可以大大减少工作量,也添加了容错率。但是会发现所有的模板和样式都放在主界面的XAML代码量会很多、很乱,所以可以采用单独的资源词典来存放这些模板和样式,主界面只要根据Key调用即可。

二、资源字典的使用

1、选中项目右键->添加->新建项->资源词典(WPF)

生成的初始资源词典如下:

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="clr-namespace:TemplateDemo">
  4. </ResourceDictionary>

现在可以在内容将模板和样式作为资源分流到各个资源词典,现在将Button的默认模板转移至该控件模板

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:local="clr-namespace:TemplateDemo">
  4.  
  5. <ResourceDictionary.MergedDictionaries>
  6. <ResourceDictionary>
  7. <Style x:Key="FocusVisual">
  8. <Setter Property="Control.Template">
  9. <Setter.Value>
  10. <ControlTemplate>
  11. <Rectangle Margin="" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="" StrokeDashArray="1 2"/>
  12. </ControlTemplate>
  13. </Setter.Value>
  14. </Setter>
  15. </Style>
  16. <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
  17. <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
  18. <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
  19. <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
  20. <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
  21. <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
  22. <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
  23. <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
  24. <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
  25. <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
  26. <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
  27. <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
  28. <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
  29. <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  30. <Setter Property="BorderThickness" Value=""/>
  31. <Setter Property="HorizontalContentAlignment" Value="Center"/>
  32. <Setter Property="VerticalContentAlignment" Value="Center"/>
  33. <Setter Property="Padding" Value=""/>
  34. <Setter Property="Template">
  35. <Setter.Value>
  36. <ControlTemplate TargetType="{x:Type Button}">
  37. <StackPanel>
  38. <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
  39. <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
  40. </Border>
  41. </StackPanel>
  42. <ControlTemplate.Triggers>
  43. <Trigger Property="IsDefaulted" Value="true">
  44. <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  45. </Trigger>
  46. <Trigger Property="IsMouseOver" Value="true">
  47. <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
  48. <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
  49. </Trigger>
  50. <Trigger Property="IsPressed" Value="true">
  51. <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
  52. <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
  53. </Trigger>
  54. <Trigger Property="IsEnabled" Value="false">
  55. <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
  56. <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
  57. <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
  58. </Trigger>
  59. </ControlTemplate.Triggers>
  60. </ControlTemplate>
  61. </Setter.Value>
  62. </Setter>
  63. </Style>
  64. </ResourceDictionary>
  65. </ResourceDictionary.MergedDictionaries>
  66. </ResourceDictionary>

2、要引用该资源字典还需要在App.Xaml中进行声明,我的名称叫TemplateDictionary.xaml,需要保证其命名空间一致

  1. <Application.Resources>
  2. <ResourceDictionary Source="TemplateDictionary.xaml"></ResourceDictionary>
  3. </Application.Resources>

3、在主XAML中使用StaticResource或DynamicResource进行静态或动态引用即可

  1. <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="309,286,0,0" VerticalAlignment="Top" Width="" Style="{StaticResource ButtonStyle1}"/>

以上就是关于获取默认空间模板和使用资源词典的一些简单的介绍,结合起来使用可以搭建简洁方便的代码布局

WPF默认控件模板的获取和资源词典的使用的更多相关文章

  1. WPF标准控件模板查看程序(文件里面)

    xaml <Window x:Class="ControlTemplateBrowser.MainWindow" xmlns="http://schemas.mic ...

  2. WPF 寻找控件模板中的元素

    <Window x:Class="Wpf180706.Window10"        xmlns="http://schemas.microsoft.com/wi ...

  3. WPF Button控件模板

     <Window x:Class="ControlTemplateDemo.MainWindow"        xmlns="http://schemas.m ...

  4. WPF中Expander的用法和控件模板详解

    一.Expander的用法 在WPF中,Expander是一个很实用的复合控件,可以很方便的实现下拉菜单和导航栏等功能.先介绍简单的用法,而后分析他的控件模板. <Window.Resource ...

  5. 【WPF学习】第六十章 创建控件模板

    经过数十天的忙碌,今天终于有时间写博客. 前面一章通过介绍有关模板工作方式相关的内容,同时介绍了FrameWorkElement下所有控件的模板.接下来将介绍如何构建一个简单的自定义按钮,并在该过程中 ...

  6. WPF数据模板和控件模板

     WPF中有控件模板和数据模板,控件模板可以让我们自定义控件的外观,而数据模板定义了数据的显示方式,也就是数据对象的可视结构,但是这里有一个问题需要考虑,数据是如何显示出来的?虽然数据模板定义了数 ...

  7. WPF控件模板

    引言:在进行WPF项目开发过程中,由于项目的需要,经常要对某个控件进行特殊的设定,其中就牵涉到模板的相关方面的内容.本文也是在自己进行项目开发过程中遇到控件模板设定时集中搜集资料后整理出来的,以供在以 ...

  8. WP8.1学习系列(第二十六章)——控件模板

    在本文中 自定义控件模板示例 指定控件的可视结构. 指定控件的可视行为 使用工具轻松处理主题 控件和辅助功能 了解有关控件默认模板的详细信息 控件模板中的主题资源 相关主题 在 XAML 框架中,如果 ...

  9. 《Programming WPF》翻译 第5章 7.控件模板

    原文:<Programming WPF>翻译 第5章 7.控件模板 如果仔细的看我们当前的TTT游戏,会发现Button对象并没有完全为我们工作.哪些TTT面板有内圆角? 图5-14 这里 ...

随机推荐

  1. CSharpGL(56)[译]Vulkan入门

    CSharpGL(56)[译]Vulkan入门 本文是对(http://ogldev.atspace.co.uk/www/tutorial50/tutorial50.html)的翻译,作为学习Vulk ...

  2. Vue小练习 03

    """ 1.有以下广告数据(实际数据命名可以略做调整) ad_data = { tv: [ {img: 'img/tv/tv1.jpg', title: 'tv1'}, ...

  3. JAVA集合框架(一)-综述

    目录 什么是java集合框架 使用类型安全的容器 集合框架简图 集合类库主要接口简述 Collection接口方法概览 什么是java集合框架 其实就是java类库提供的一套相当完整的各种数据结构的实 ...

  4. .NET轻松实现支付宝服务窗网页授权并获取用户相关信息

    前言: 最近在开发一个商业街区的聚合扫码支付功能,其中需要用到的有支付宝,微信两种支付方式,当然对于开发微信支付而已作为自己的老本行已经比较熟悉了,然而对于我来说支付宝支付还是头一次涉及到.这次项目中 ...

  5. 阿里面试实战题1----TreeSet,HashSet 区别

    TreeSet,HashSet 区别 TreeSet public class TreeSet<E> extends AbstractSet<E> implements Nav ...

  6. Kotlin Parameter specified as non-null is null

    报错信息如下: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.j ...

  7. 如何通过RMAN使用传输表空间迁移到不同的Endian平台 (Doc ID 371556.1)

    How to Migrate to different Endian Platform Using Transportable Tablespaces With RMAN (Doc ID 371556 ...

  8. linux里如何配置本地yum源和外网yum源

    一:本地和外网源配置方法 二:外网YUM源的地址 一: ① 本地源配置方法:以光盘里rpm举例(这里使用虚拟机演示) 1.挂载一个iso的镜像 把光盘挂载到一个目录里,然后进入/etc/yum.rep ...

  9. Linux系统学习 三、网络基础—虚拟机网络配置

    Linux网络基础 Linux的ip地址配置 Linux网络配置文件 常用网络命令 虚拟机网络参数配置 1.配置Linux的IP地址 首先配置一个IP地址,让IP生效. ifconfig查看当前网络配 ...

  10. nginx配合gotty的websocket连接配置

    由于gotty我作了url加密, 所以url在nginx里的前缀要能替换才行. 配置关键就在于有没有/ #user nobody; worker_processes 1; #error_log log ...