在WPF里面每个控件都有一个默认的模板,如果我们想要该变控件格式的话,就要应用到这个ControlTemplate(控件模板)了,在更换控件的格式,我们只需要声明一个ControlTemplate对象,在ControlTemplate对象里面进行相应的配置就行了,最后在将你配置好的属性赋值给控价就行了。
下面我们通过代码实践一下:
重写Button按钮模板
<Window x:Class="高级动画.控件模板" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="控件模板" Height="300" Width="300"> <Window.Resources> <!--重写模板--> <ControlTemplate x:Key="btn" TargetType="Button"> <Button x:Name="btns"> <!--背景渐变--> <Button.Background> <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> <GradientStop Color="#FF5D88C7" Offset="0.5"/> <GradientStop Color="#FF4375BD" Offset="0.51"/> </LinearGradientBrush> </Button.Background> </Button> </ControlTemplate> </Window.Resources> <Grid> <Button Template="{StaticResource btn}"></Button> </Grid> </Window>
重写前如图所示:
重写后如图所示:
由于我们只给了Button按钮添加了一个背景渐变的样式,所以前后两张图片只有背景样式改变,如果还想要继续改变Button控件的模板,只需要在里面继续添加你需要重写的模板就行了。
发表评论 取消回复