控件模板,也是一种资源。
1.建立控件模板的资源
<Window.Resources>
<ControlTemplate x:Key="a" TargetType="{x:Type Button}">
<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red" TextBlock.Foreground="White">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center">
</ContentPresenter>
</Border>
</ControlTemplate>
</Window.Resources>
2.对控件模板进行调用
<Button Content="Button" Template="{StaticResource a}" HorizontalAlignment="Left" Margin="176,258,0,0" VerticalAlignment="Top" Width="75"/>
可以看到,控件模板也可以向样式一样使用。
他们的区别是什么。
样式:
1.样式设置,用来为一组相同控件设置统一的样式
2.只能改变控件的已有属性值(比如颜色字体)
3.样式里面可以包含控件模板
控件模板:
1.用来为一组相同控件设置统一的外形
2.只能在Border里面增加元素。可以增加任何控件
例如代码
对按钮进行设置了控件模板,按钮变成了“四不像”
<Window.Resources>
<ControlTemplate x:Key="a" TargetType="{x:Type Button}">
<Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red" TextBlock.Foreground="White">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Test">
</TextBlock>
<Button Content="123"></Button>
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center">
</ContentPresenter>
</StackPanel>
</Border>
</ControlTemplate>
</Window.Resources>