Template简介

it2022-06-24  203

分类

  ControlTemplate ItemsPanelTemplate DataTemplate

样式Style和模板Template对比

Style:样式,风格Template:模版,某种控件长什么样子Style依赖原始控件,对控件的已有属性进行改造Template天马行空,实现对控件颠覆式的改造Style中可以包含Template

Style嵌套Template

<Window.Resources> <Style TargetType="CheckBox" x:Key="CheckBoxStyle"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="CheckBox"> <StackPanel> <!--设置Rectangle边框的宽度和颜色,设置Rectangle的高宽--> <Rectangle Name="breakRectangle" Stroke="Cyan" StrokeThickness="2" Width="20" Height="20"> <Rectangle.Fill> <!--默认Rectangle填充色为White--> <SolidColorBrush Color="White"></SolidColorBrush> </Rectangle.Fill> </Rectangle> <!--没有ContentPresenter标签,则不保留原来的属性,显示不出文本--> <!--TemplateBinding Padding,则绑定每个CheckBox自己的Margin,更灵活--> <ContentPresenter Margin="{TemplateBinding Padding}"></ContentPresenter> </StackPanel> <ControlTemplate.Triggers> <!--目标为breakRectangle,当IsChecked=true的时候,将填充色设置为CornflowerBlue--> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="breakRectangle" Property="Fill" Value="CornflowerBlue"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <CheckBox Style="{StaticResource CheckBoxStyle}"></CheckBox> </Grid>

示例代码

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

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

相关资源:数据结构—成绩单生成器

最新回复(0)