ControlTemplate
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的更多相关文章
- 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate
[源码下载] 背水一战 Windows 10 (6) - 控件 UI: 字体的自动继承的特性, Style, ControlTemplate 作者:webabcd 介绍背水一战 Windows 10 ...
- 控件 UI: 字体的自动继承的特性, Style, ControlTemplate
字体的自动继承的特性 Style 样式 ControlTemplate 控件模板 示例1.演示字体的自动继承的特性Controls/UI/FontInherit.xaml <Page x:Cla ...
- WPF:在ControlTemplate中使用TemplateBinding
A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to w ...
- WPF中ControlTemplate和DataTemplate的区别
下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...
- 从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第四讲 WPF中 ControlTemplate
上讲我们介绍了DataTemplate,现在我们就介绍下ControlTemplate,可能后面大多在编码时候会出现一些英文,工作习惯,请见谅. ControlTemplate: 控件的外观,也就是控 ...
- Controltemplate datatemplate
DataTemplate ControlTemplate we can search many posts about this topic. some valuable link: DataTemp ...
- DataTemplate和ControlTemplate联系与区别
---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两 ...
- DataTemplate和ControlTemplate的关系
DataTemplate和ControlTemplate的关系(转载自haiziguo) 一.ContentControl中的DataTemplate 在开始之前,我们先去看一下ContentCont ...
- 访问ControlTemplate内部的元素
需要用到code behind 注意要给需要访问的元素命名x:Name="PART_TextBlock" <ResourceDictionary xmlns="ht ...
- WPF中的ControlTemplate(控件模板)(转)
原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html WPF中的ControlTemplate(控件模板) ...
随机推荐
- 定义变量let,const
1.块级作用域let 声明变量,作用域是最近的"{}": 'use strict'; { let test = '1'; } console.log(test);//test is ...
- 【codeforces 760C】Pavel and barbecue
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 你的薪水增速跑赢GDP了没
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Color the ball(杭电1556)
Color the ball Time Limit : 9000/3000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- WPF Chart 图标
DevExpress: <dxc:ChartControl.Diagram> <dxc:XYDiagram2D.SeriesTemplate> </dxc:XYDiagr ...
- 详细回复某个CSDN网友,对我的文章和技术实力以及CSDN的吐槽
貌似被大学生鄙视了,我也是醉了,现在的大学生水平和信心,都这么高了~ 看来,我得加把劲了~ o(︶︿︶)o 电子商务系列文章,是我闲来无事,分享自己的一些业余实践经验的文章.其中关于数据库设计的这一篇 ...
- solr 7.x 查询及高亮
查询时的api分为两种一种是万能的set,还有一种是setxxxquery @Test public void search2() throws Exception{ HttpSolrClient s ...
- hdu3698 Let the light guide us dp+线段树优化
http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...
- List<Map<String, String>> 开启 Map<String, List<String>>
将List变成Map结构体,下面的文字是没有水平! 写作方法传送前土壤很长一段时间.我不知道有没有好的解决办法.我们也希望提供! Map<String, String> map1 = ne ...
- @AspectJ support (good)
AspectJ类型匹配的通配符:*:匹配任何数量字符:..:匹配任何数量字符的重复,如在类型模式中匹配任何数量子包:而在方法参数模式中匹配任何数量参数.+:匹配指定类型的子类型:仅能作为后缀放在类型模 ...