ControlTemplate:外观定制

<Window.Resources>
<ControlTemplate x:Key="CheckBoxControlTemplate" TargetType="CheckBox">
<StackPanel>
<Rectangle Name="breakRectangle" Stroke="Red" StrokeThickness="2" Width="20" Height="20">
<Rectangle.Fill>
<!--默认Rectangle填充色为White-->
<SolidColorBrush Color="White"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
<!--ContentPresenter 保留原控件属性-->
<ContentPresenter Margin="{TemplateBinding Padding}"></ContentPresenter>
</StackPanel>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<CheckBox Content="我是普通CheckBox"></CheckBox>
<CheckBox Grid.Row="1" Template="{StaticResource CheckBoxControlTemplate}" Content="我是模板CheckBox"></CheckBox>
<CheckBox Grid.Row="2" Template="{StaticResource CheckBoxControlTemplate}" Content="我是模板CheckBox,我跟上一个模板的区别在于Padding" Padding="15"></CheckBox>
</Grid>

Tips

<ContentPresenter Margin="{TemplateBinding Padding}"/>

ContentPresenter 保留原控件属性
TemplateBinding Padding,即绑定每个CheckBox自己的Margin,更灵活

效果

 

此时,点击Rectangle是没有效果的

ControlTemplate中使用触发器

<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="breakRectangle" Property="Fill" Value="Coral"></Setter>
</Trigger>
</ControlTemplate.Triggers>

Setter可以选择TargetName,即一个控件的触发器可以修改另一个控件的属性

效果

 
 

使用Brush

Brush一类画笔,需定义为对象才能使用
可以使用颜色、图片等等

<Window.Resources>
<ControlTemplate x:Key="CheckBoxControlTemplate" TargetType="CheckBox">
<ControlTemplate.Resources>
<!--颜色-->
<SolidColorBrush x:Key="ColorBrush" Color="Pink"></SolidColorBrush>
<!--图片-->
<ImageBrush x:Key="ImageBrush" ImageSource="Images/cat.jpg"></ImageBrush>
</ControlTemplate.Resources>
<StackPanel>
<Rectangle Name="breakRectangle" Stroke="OrangeRed" StrokeThickness="2" Width="100" Height="100">
<Rectangle.Fill>
<!--默认Rectangle填充色为White-->
<SolidColorBrush Color="White"></SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
<!--ContentPresenter 保留原控件属性-->
<ContentPresenter Margin="{TemplateBinding Padding}"></ContentPresenter>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="breakRectangle" Property="Fill" Value="{StaticResource ImageBrush}"></Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="breakRectangle" Property="Fill" Value="{StaticResource ColorBrush}"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"></RowDefinition>
<RowDefinition Height="0.5*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="没有选中时,CheckBox填充色为Pink"></Label>
<Label Grid.Row="1" Content="选中时,CheckBox填充为猫猫的图片"></Label>
<CheckBox Grid.Row="2" Template="{StaticResource CheckBoxControlTemplate}" Content="我是模板CheckBox"></CheckBox>
</Grid>

效果

 
 
 

可以对比在Style中嵌套Template的写法

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Template/StyleUseTemplate

其他例子

<Window.Resources>
<ControlTemplate x:Key="CheckBoxControlTemplate" TargetType="{x:Type CheckBox}">
<Grid>
<TextBlock x:Name="textBlock1" HorizontalAlignment="Left" Margin="116,39,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
<ContentPresenter/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="textBlock1" Property="Text" Value="CheckBox is checked."></Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="textBlock1" Property="Text" Value="CheckBox is not checked."></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<CheckBox Template="{StaticResource CheckBoxControlTemplate}" x:Name="checkBox1" Content="CheckBox" HorizontalAlignment="Left" Margin="137,59,0,0" VerticalAlignment="Top"></CheckBox>
</Grid>

对非内置属性的修改,用模板
用style识别不了textBlock1
直接写CheckBox 的Triggers识别不了IsChecked

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Template/ControlTemplate

ControlTemplate的更多相关文章

  1. 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate

    [源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...

  2. 控件 UI: 字体的自动继承的特性, Style, ControlTemplate

    字体的自动继承的特性 Style 样式 ControlTemplate 控件模板 示例1.演示字体的自动继承的特性Controls/UI/FontInherit.xaml <Page x:Cla ...

  3. WPF:在ControlTemplate中使用TemplateBinding

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

  4. WPF中ControlTemplate和DataTemplate的区别

    下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...

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

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

  6. Controltemplate datatemplate

    DataTemplate ControlTemplate we can search many posts about this topic. some valuable link: DataTemp ...

  7. DataTemplate和ControlTemplate联系与区别

    ---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两 ...

  8. DataTemplate和ControlTemplate的关系

    DataTemplate和ControlTemplate的关系(转载自haiziguo) 一.ContentControl中的DataTemplate 在开始之前,我们先去看一下ContentCont ...

  9. 访问ControlTemplate内部的元素

    需要用到code behind 注意要给需要访问的元素命名x:Name="PART_TextBlock" <ResourceDictionary xmlns="ht ...

  10. WPF中的ControlTemplate(控件模板)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板)     ...

随机推荐

  1. centos7安装jdk+tomcat+nginx+mysql

    公司新项目要在linux下部署,搭建一下java运行环境,记录一下. 一.安装mysql 1去官网下载mysql,下载后并解压,我把mysql安装在/usr/local/mysql路径下 -linux ...

  2. nginx源代码分析--ngx_http_optimize_servers()函数

    这个函数做了连部分工作:1)以port为入口点 将实用的信息存放到hash表内 2)调用ngx_http_init_listening()函数 对port进行监听 1. 在ngx_http_core_ ...

  3. Tampermonkey版Vimium

    Tampermonkey版Vimium https://zhuanlan.zhihu.com/p/27222664

  4. 【54.38%】【BZOJ 4300】绝世好题

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1120  Solved: 609 [Submit][Status][Discuss] Descript ...

  5. signed 与 unsigned 有符号和无符号数

    unsigned int a = 0; unsigned int b = -1; // b 为 0xffffffff unsigned int c = a - 1; // c 为 0xffffffff

  6. Android6.0动态申请权限那些坑--以及避免用户选择不再提示后无法获取权限的问题

    Android 6.0 为了保护用户隐私,将一些权限的申请放在了应用运行的时候去申请, 比如以往的开发中,开发人员只需要将需要的权限在清单文件中配置即可,安装后用户可以在设置中的应用信息中看到:XX应 ...

  7. MySQL crash-safe replication

    MySQL数据库的成功离不开其replicaiton,相对于Oracle DG和Microsoft SQL Server Log Shipping来说,其简单易上手,基本上1,2分钟内根据手册就能完成 ...

  8. [SVG] Add an SVG as a Background Image

    Learn how to set an SVG as the background image of an element. Background images can be resized by c ...

  9. Java 中StringBuffer与StringBuilder区别(转)及String类的一些基本操作代码

    String 字符串常量StringBuffer 字符串变量(线程安全)  多个线程访问时,不会产生问题(Synchronized)StringBuilder 字符串变量(非线程安全) 多个线程访问时 ...

  10. N 个互异数的数组的平均逆序数

    N 个互异数的数组的平均逆序数为 N(N−1)/4 1. 简单证明 对于任意的数的表 L(5,8,9,6,4),以及其反序表 Lr(4,6,9,8,5),它们各自的逆序数分别为:6 ((5, 4), ...