A template is another style of control that differs from the style (style) in that it allows existing controls to combine a new control style
So first look at the simplest template code
XAML Code
<button content="Button"height=" at"Horizontalalignment=" Left"Margin="44,30,0,0"Name="button1"Verticalalignment="Top"Width=" the"> <Button.Template> <ControlTemplate> <border Borderthickne ss="3"Borderbrush="#FF6969FF"> <Border.Background> <lineargradientbrush endpoint="0.5,1"Startpoint="0.5,0"> <gradientstop color="#FF6F97FF"offset="0"/> <gradientstop color="#FF0021FF"offset="0.574"/> <gradientstop color="#FF6C7BFF"offset="1"/> </LinearGradientBrush> </Border.Background> <textblock text="Button"Horizontalalignment="Center"VerticalAlignment="Center"Foreground="Lime"></TextBlock> </Border> </ControlTemplate> </Button.Template> </Button>
That's a little more complicated
<controltemplate x:key="TPE"> <border borderthickness="3"Borderbrush="#FF6969FF"> <Border.Background> <lineargradientbrush endpoint="0.5,1"Startpoint="0.5,0"> <gradientstop color="#FF6F97FF"offset="0"/> <gradientstop color="#FF0021FF"offset="0.574"/> <gradientstop color="#FF6C7BFF"offset="1"/> </LinearGradientBrush> </Border.Background> <CONTENTPR Esenter verticalalignment="{TemplateBinding VerticalAlignment}"HorizontalAlignment="Center"></ContentPresenter> </Border> </ControlTemplate> <button content="Button"Template="{StaticResource TPE}"height=" at"HorizontalAlignment=" Left"margin="44,30,0,0"Name="button1"VerticalAlignment="Top"Width=" the">
ContentPresenter represents a mapping method for the content property of a button
So the purple code has a templatebinding. It indicates that the value of the verticalalignment that specifies a correlation property mapping Map button can also be margin mapped padding because they are two compatible
In style you can use template
XAML Code
<style x:key="S1"Targettype="Button"> <setter property="Template"Value="{StaticResource TPE}"></Setter> </Style> <button content="Button"style="{StaticResource S1}"height=" at"HorizontalAlignment=" Left"margin="44,30,0,0"Name="button1"VerticalAlignment="Top"Width=" the">
However, if you set the value of a property in the style, the value of the TemplateBinding mapping for the template does not map on the control.
Silverlight templates (template) use