WPF ControlTemplate
ControlTemplate:控件模板,顾名思义也就是定制特定的控件供公共调用,有点类似WinForm中对一些通用控件进行重写使用。
ControlTemplate:控件模板主要有两个重要属性:VisualTree内容属性和Triggers触发器。定义控件模板也是对控件的视觉树和触发器进行重新定义,属性可以依赖原有控件的属性,只是根据需要调整的部分属性,
在Xaml文件的Resources中定义需要使用的模板,在需要使用模板的控件中将Template赋值为相应的控件模板x:key值实现控件的重定义。
以我们最常使用的Button为例,一般也都会都Button的控件模板作为资源进行全局通用资源,在具体使用的地方进行替换原有控件的Template:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Width" Value=""/>
<Setter Property="Height" Value=""/>
</Style>
<ControlTemplate TargetType="Button" x:Key="BtnTemplate">
<Grid>
<Ellipse Name="FaceEllispe" Width="{TemplateBinding Button.Width}" Height="{TemplateBinding Button.Height}" Fill="{TemplateBinding Button.Background}"/>
<TextBlock Name="txtBlock" Margin="{TemplateBinding Button.Padding}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Button.Content}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock Style="{StaticResource TitleText}" Name="textblock1">My Test WPF</TextBlock>
<TextBlock>Click on my new Button</TextBlock>
<Button Content="Test btn" Template="{StaticResource BtnTemplate}"/>
</StackPanel>
</Grid>
</Window>
对于页面中特定的某个Button进行结构重定义的话,也可以直接在Button控件下面改写Template:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Width" Value=""/>
<Setter Property="Height" Value=""/>
</Style>
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock Style="{StaticResource TitleText}" Name="textblock1">My Test WPF</TextBlock>
<TextBlock>Click on my new Button</TextBlock>
<Button Content="Test btn">
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse Name="FaceEllispe" Width="{TemplateBinding Button.Width}" Height="{TemplateBinding Button.Height}" Fill="{TemplateBinding Button.Background}"/>
<TextBlock Name="txtBlock" Margin="{TemplateBinding Button.Padding}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Button.Content}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Foreground" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Grid>
</Window>
两种实现方式,采用哪种依赖于模板的通用性,当然即使都是单次使用也可以写成资源文件进行调用,简化Xaml复杂度。
WPF ControlTemplate的更多相关文章
- 深入详解WPF ControlTemplate
WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下WPF ControlTemplate. 其实WPF的每一个控件都有一 ...
- [No0000DA]WPF ControlTemplate简介
一.简介 WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下ControlTemplate.其实WPF的每一个控件都有一 ...
- WPF : ControlTemplate和DataTemplate的区别
ControlTemplate用于描述控件本身. 使用TemplateBinding来绑定控件自身的属性, 比如{TemplateBinding Background}DataTemplate用于描述 ...
- WPF ControlTemplate 动画板 结束事件不触发
解决此问题很简单 将Storyboard单独提取出来及可 给定Key名称,然后在触发器中的BeginStoryboard的storyboard绑定即可 <!--单独提取并设置Xkey--> ...
- WPF ControlTemplate,DataTemplate
The Control Template defines the visual appearance of a control. All of the UI elements have some ki ...
- WPF DataTemplate與ControlTemplate
一. 前言 什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...
- wpf的控件style
前段时间一直在做wpf的UI开发,每次想做些控件style定制的时候都很头疼 很多控件不知道他的controltemplate是什么样的 为了方便大家写style 特别奉上wpf的style大全 从此 ...
- WPF:在ControlTemplate中使用TemplateBinding
A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to w ...
- 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第四讲 WPF中 ControlTemplate
上讲我们介绍了DataTemplate,现在我们就介绍下ControlTemplate,可能后面大多在编码时候会出现一些英文,工作习惯,请见谅. ControlTemplate: 控件的外观,也就是控 ...
随机推荐
- 强大的VS插件CodeRush发布v19.1.4|支持Visual Studio 2019
CodeRush是一个强大的Visual Studio .NET 插件,它利用整合技术,通过促进开发者和团队效率来提升开发者体验. [CodeRush for Visual Studio v19.1. ...
- 洛谷P3768 简单的数学题 莫比乌斯反演+杜教筛
题意简述 求出这个式子 \[ \sum_{i=1}^n\sum_{j=1}^n ij(i,j) \bmod p \] 做法 先用莫比乌斯反演拆一下式子 \[ \begin{split} \sum_{i ...
- 设计模式Design Pattern(3) -- 责任链模式
什么是责任链模式? 责任链模式(Chain of Responsibility Pattern):请求知道公开接口,但不知道那个具体类处理,这些具体处理类对象连接成一条链.请求沿着这条链传递,直到有对 ...
- Python---进阶---捕获异常
一.编写一个计算减法的方法,当第一个数小于第二个数时,抛出“被减数不能小于减数”的异常 ------------------------------------------------- def ji ...
- JS audio播放一个的时候,其他正在播放的关闭
audio在使用中,如果有多个,在播放的时候,如果一个声音没有播放完继续下一个的话,原来正在播放的并不会关闭(在Android和PC上测试是这样,苹果产品不清楚) 现在需要做的是,当播放其中一个的时候 ...
- [CSP-S模拟测试]:题(DP+数学)
题目描述 出个题就好了.这就是出题人没有写题目背景的原因.你在平面直角坐标系上.你一开始位于$(0,0)$.每次可以在上/下/左/右四个方向中选一个走一步.即:从$(x,y)$走到$(x,y+1),( ...
- tp5关联模型进行条件查询
public function wordOne(){ return $this->hasOne('TeachWord','id','w_id')->field('id,pid,title' ...
- 使用Mybatis执行sql脚本
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- WebRtc 源码下载
项目需要用到WebRtc,记录下基本下载的步骤: 1.下载depot_tools,利用depot_tools 下载WebRtc源码 git clone https://chromium.googles ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...