WPF默认控件模板的获取和资源词典的使用
一、获取默认的控件模板
WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例):
1、创建一个Button
2、在界面上选择Button,右键->编辑模板->编辑副本 ,即可看到XAML中自动生成了原始的控件模板
3、可以在默认模板上修改其中的一些属性运行测试是否生效
这样在默认的控件模板上编辑,只修改需要修改的部分即可,可以大大减少工作量,也添加了容错率。但是会发现所有的模板和样式都放在主界面的XAML代码量会很多、很乱,所以可以采用单独的资源词典来存放这些模板和样式,主界面只要根据Key调用即可。
二、资源字典的使用
1、选中项目右键->添加->新建项->资源词典(WPF)
生成的初始资源词典如下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TemplateDemo">
</ResourceDictionary>
现在可以在内容将模板和样式作为资源分流到各个资源词典,现在将Button的默认模板转移至该控件模板
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TemplateDemo"> <ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
<Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value=""/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value=""/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel>
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
2、要引用该资源字典还需要在App.Xaml中进行声明,我的名称叫TemplateDictionary.xaml,需要保证其命名空间一致
<Application.Resources>
<ResourceDictionary Source="TemplateDictionary.xaml"></ResourceDictionary>
</Application.Resources>
3、在主XAML中使用StaticResource或DynamicResource进行静态或动态引用即可
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="309,286,0,0" VerticalAlignment="Top" Width="" Style="{StaticResource ButtonStyle1}"/>
以上就是关于获取默认空间模板和使用资源词典的一些简单的介绍,结合起来使用可以搭建简洁方便的代码布局
WPF默认控件模板的获取和资源词典的使用的更多相关文章
- WPF标准控件模板查看程序(文件里面)
xaml <Window x:Class="ControlTemplateBrowser.MainWindow" xmlns="http://schemas.mic ...
- WPF 寻找控件模板中的元素
<Window x:Class="Wpf180706.Window10" xmlns="http://schemas.microsoft.com/wi ...
- WPF Button控件模板
<Window x:Class="ControlTemplateDemo.MainWindow" xmlns="http://schemas.m ...
- WPF中Expander的用法和控件模板详解
一.Expander的用法 在WPF中,Expander是一个很实用的复合控件,可以很方便的实现下拉菜单和导航栏等功能.先介绍简单的用法,而后分析他的控件模板. <Window.Resource ...
- 【WPF学习】第六十章 创建控件模板
经过数十天的忙碌,今天终于有时间写博客. 前面一章通过介绍有关模板工作方式相关的内容,同时介绍了FrameWorkElement下所有控件的模板.接下来将介绍如何构建一个简单的自定义按钮,并在该过程中 ...
- WPF数据模板和控件模板
WPF中有控件模板和数据模板,控件模板可以让我们自定义控件的外观,而数据模板定义了数据的显示方式,也就是数据对象的可视结构,但是这里有一个问题需要考虑,数据是如何显示出来的?虽然数据模板定义了数 ...
- WPF控件模板
引言:在进行WPF项目开发过程中,由于项目的需要,经常要对某个控件进行特殊的设定,其中就牵涉到模板的相关方面的内容.本文也是在自己进行项目开发过程中遇到控件模板设定时集中搜集资料后整理出来的,以供在以 ...
- WP8.1学习系列(第二十六章)——控件模板
在本文中 自定义控件模板示例 指定控件的可视结构. 指定控件的可视行为 使用工具轻松处理主题 控件和辅助功能 了解有关控件默认模板的详细信息 控件模板中的主题资源 相关主题 在 XAML 框架中,如果 ...
- 《Programming WPF》翻译 第5章 7.控件模板
原文:<Programming WPF>翻译 第5章 7.控件模板 如果仔细的看我们当前的TTT游戏,会发现Button对象并没有完全为我们工作.哪些TTT面板有内圆角? 图5-14 这里 ...
随机推荐
- 用dotnet core 搭建web服务器(一)http server
环境说明 dotnet core,开发需要安装dotnetcore sdk,运行需要安装 dotnetcore runtime 运行目前几乎支持所有常见平台 开发推荐windows10 平台 首先安装 ...
- ETCD:实验特性和APIs
原文地址:Experimental features and APIs 大多数情况下,etcd项目是稳定的,但我们仍在快速发展! 我们相信快速发布理念. 我们希望获得有关仍在开发和稳定中的功能的早期反 ...
- VUE Error: listen EADDRNOTAVAIL: address not available
vue项目运行npm run dev时出现 Error: listen EADDRNOTAVAIL: address not available ... 的错误 原因:配置的IP不对 解决方法:打开c ...
- 什么是DaemonSet
DaemonSet只管理Pod对象,通过nodeAffinity和Toleration两个调度器,保证每个节点上只有一个Pod 集群动态加入了新Node,DaemonSet中的Pod也会添加在新加入N ...
- C#_.NetFramework_WebAPI项目_EXCEL数据导出
[推荐阅读我的最新的Core版文章,是最全的介绍:C#_.NetCore_Web项目_EXCEL数据导出] 项目需要引用NPOI的Nuget包: A-2--EXCEL数据导出--WebAPI项目--N ...
- IDEA 2019.2版本下载安装与PJ教程
场景 IDEA版本过低的话会导致某些IDEA插件没法安装,比如Lombok插件和EasyCode插件等. 实现 双击exe安装包 点击Next 选择安装路径,点击Next 设置桌面快捷方式,增加到右键 ...
- 从华为“鸿蒙”备胎看IT项目建设
别误会啊,本文并不在讲大家在做IT项目建设的时候学华为做一个备胎系统,以防正主系统崩掉之后能够及时替换到备胎系统里面,不影响业务. 前段时间华为被美帝制裁,然后各家组织对华为各种限制.然而华为整体布局 ...
- Android 开发时使用 ViewPager 的问题及解决方案整理
1. ViewPager 的页面重置问题 当我们使用ViewPager控件时,假设我们的ViewPager有三页,当我们第一次启动ViewPager显示第一页的时候,ViewPager会预加载第二页, ...
- 【Gradle】Gradle插件
Gradle插件 插件的作用 把插件应用到项目中,插件会扩展项目的功能,帮助在项目构建过程中做很多事情. 1.可以添加任务到项目中,帮助完成测试.编译.打包等. 2.可以添加依赖配置到项目中,可以通过 ...
- ActiveMQ持久化到MySQL以及使用SSL协议通讯
最近公司事情稍微少了点,研究下怎么优化几个系统的交互,因为我们目前使用的是长链接的同步接口,就考虑用下MQ来处理下.由于公司对安全有要求且和CA业务有关,则使用了SSL协议.此文使用的是Activem ...