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的更多相关文章

  1. 深入详解WPF ControlTemplate

    WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下WPF ControlTemplate. 其实WPF的每一个控件都有一 ...

  2. [No0000DA]WPF ControlTemplate简介

    一.简介 WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下ControlTemplate.其实WPF的每一个控件都有一 ...

  3. WPF : ControlTemplate和DataTemplate的区别

    ControlTemplate用于描述控件本身. 使用TemplateBinding来绑定控件自身的属性, 比如{TemplateBinding Background}DataTemplate用于描述 ...

  4. WPF ControlTemplate 动画板 结束事件不触发

    解决此问题很简单 将Storyboard单独提取出来及可 给定Key名称,然后在触发器中的BeginStoryboard的storyboard绑定即可 <!--单独提取并设置Xkey--> ...

  5. WPF ControlTemplate,DataTemplate

    The Control Template defines the visual appearance of a control. All of the UI elements have some ki ...

  6. WPF DataTemplate與ControlTemplate

    一. 前言     什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...

  7. wpf的控件style

    前段时间一直在做wpf的UI开发,每次想做些控件style定制的时候都很头疼 很多控件不知道他的controltemplate是什么样的 为了方便大家写style 特别奉上wpf的style大全 从此 ...

  8. WPF:在ControlTemplate中使用TemplateBinding

    A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to w ...

  9. 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第四讲 WPF中 ControlTemplate

    上讲我们介绍了DataTemplate,现在我们就介绍下ControlTemplate,可能后面大多在编码时候会出现一些英文,工作习惯,请见谅. ControlTemplate: 控件的外观,也就是控 ...

随机推荐

  1. Django【第21篇】:Ajax之FormData

    ajax补充--------FormData等... 一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000 ...

  2. 【SaltStack官方版】—— states教程, part 3 - 定义,包括,延伸

    STATES TUTORIAL, PART 3 - TEMPLATING, INCLUDES, EXTENDS 本教程建立在第1部分和第2部分涵盖的主题上.建议您从此开始.这章教程我们将讨论更多  s ...

  3. LTE抛弃了CDMA?

    原文链接:https://blog.csdn.net/readhere/article/details/82764919 本文节选自<LTE教程:结构与实施> 大家都听说过这样的说法:LT ...

  4. MIF文件生成说明

    mif文件就是存储器初始化文件,即memory initialization file,用来配置RAM或ROM中的数据.生成QuartusII11.0可用的mif文件,有如下几种方式: 方法1:利用Q ...

  5. BZOJ 1304: [CQOI2009]叶子的染色 树形DP + 结论

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...

  6. 【BZOJ2200】道路和航线(并查集,拓扑排序,最短路)

    题意:n个点,有m1条双向边,m2条单向边,双向边边长非负,单向边可能为负 保证如果有一条从x到y的单项边,则不可能存在从y到x的路径 问从S出发到其他所有点的最短路 n<=25000,n1,m ...

  7. jdk7.x对Jenkins上的SonarQube Plugin的支持不足,替换方式

    Jenkins.war放在Tomcat7下,完成各种配置,包括Jenkins中JDK,Maven,Git等. 最初的配置为Tomcat7, JDK7.x. 因为要在Jenkins上安装SonarQub ...

  8. 170814-17关于javaweb的知识点

    1.   静态web项目.动态web项目区别                                         WEB-INF                               ...

  9. 学习wavenet_vocoder之预处理、训练

    一.预处理 1.在进行预处理时,如果不明白需要的参数,可以使用命令获取帮助,从这里我们可以看到可以具体的用法和对应的参数. python preprocess.py --help python pre ...

  10. [CSP-S模拟测试]:d(贪心+树状数组)

    题目传送门(内部题65) 输入格式 第一行,一个自然数$T$,代表数据组数.对于每组数据:第一行,一个正整数$n$,一个自然数$m$.接下来$n$行,每行两个正整数,$a_i,b_i$. 输出格式 对 ...