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

转载于:https://www.cnblogs.com/Lulus/p/8157711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值