继上一篇文章提到的,这里我们讲一下如何如何满足一些用户变态要求,将控件形状改变,并且我们顺手给他来个背景色触发变化。
我们知道从控件类继承的类具有控件模板(ControlTemplate),它用于定义控件(Control)的结构和外观。控件的模板属性是公共的,因此我们可以为控件指定非默认控件模板。通常我们将控件指定新的控件模板以自定义控件的外观.同样,我们还是以上一章节的代码为例:
完整代码如下:
1 <Window x:Class="WpfControls.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Window.Resources> 6 <Style TargetType="Button"> 7 <Setter Property="Template"> 8 <Setter.Value> 9 <ControlTemplate TargetType="Button"> 10 <Border x:Name="Border" CornerRadius="20" BorderBrush="PaleGreen" BorderThickness=" 1"> 11 <Border.Background> 12 <LinearGradientBrush StartPoint=" 0,0.5" EndPoint=" 1,0.5" Opacity=" 0.5"> 13 <GradientStop Color="Green" Offset=" 0.0"/> 14 <GradientStop Color="White" Offset=" 1.0"/> 15 </LinearGradientBrush> 16 </Border.Background> 17 <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/> 18 </Border> 19 <ControlTemplate.Triggers> 20 <Trigger Property="IsPressed" Value="true"> 21 <Setter TargetName="Border" Property="Background"> 22 <Setter.Value> 23 <LinearGradientBrush StartPoint=" 0,0.5" EndPoint="1,0.5"> 24 <GradientStop Color="Green" Offset="0.0"/> 25 <GradientStop Color="Red" Offset="0.5"/> 26 </LinearGradientBrush> 27 </Setter.Value> 28 </Setter> 29 </Trigger> 30 </ControlTemplate.Triggers> 31 </ControlTemplate> 32 </Setter.Value> 33 </Setter> 34 </Style> 35 </Window.Resources> 36 <Grid> 37 <Grid.Background> 38 <LinearGradientBrush StartPoint="0,1.5" EndPoint="0.5,0.1" Opacity="0.2"> 39 <LinearGradientBrush.GradientStops> 40 <GradientStop Color="Green" Offset="0.0"/> 41 <GradientStop Color="LightBlue" Offset="0.75"/> 42 <GradientStop Color="LightCoral" Offset="1.2"/> 43 </LinearGradientBrush.GradientStops> 44 </LinearGradientBrush> 45 </Grid.Background> 46 <Grid.RowDefinitions> 47 <RowDefinition Height="30"/> 48 <RowDefinition Height="30"/> 49 <RowDefinition Height="30"/> 50 </Grid.RowDefinitions> 51 <Grid.ColumnDefinitions> 52 <ColumnDefinition/> 53 <ColumnDefinition/> 54 </Grid.ColumnDefinitions> 55 <Label Grid.Column="0" FontSize="14" FontWeight="Bold">Entry your First Name:</Label> 56 <TextBox Grid.Row="0" Grid.Column="1" Name="firstName" Margin="0,5,10,5"/> 57 <Label Grid.Row="1">Entry your Last Name:</Label> 58 <TextBox Grid.Column="1" Grid.Row="1" Name="lastName" Margin="0,5,10,5"/> 59 <Button Grid.Row="2" Grid.Column="0" Name="submit" Margin="2" >View message</Button> 60 <Button Grid.Row="2" Grid.Column="2" Name="Clear" Margin="2">Clear Name</Button> 61 </Grid> 62 </Window>
编译运行效果:
按下按键
首先我们同样是在windows框架上定义我们的style(样式) Window.Resources ,选择控件类型为按键控件 <Style TargetType="Button"> 紧接着设置属性模板 <Setter Property="Template"> 这里运用Border来设置边框属性外观默认参数:
<Border x:Name="Border" CornerRadius="20" BorderBrush="PaleGreen" BorderThickness=" 1"> <Border.Background> <LinearGradientBrush StartPoint=" 0,0.5" EndPoint=" 1,0.5" Opacity=" 0.5"> <GradientStop Color="Green" Offset=" 0.0"/> <GradientStop Color="White" Offset=" 1.0"/> </LinearGradientBrush> </Border.Background> <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/> </Border>
接着定义一触发点,触发为判断是否按下按钮(IsPressed)
<ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="true"> <Setter TargetName="Border" Property="Background"> <Setter.Value> <LinearGradientBrush StartPoint=" 0,0.5" EndPoint="1,0.5"> <GradientStop Color="Green" Offset="0.0"/> <GradientStop Color="Red" Offset="0.5"/> </LinearGradientBrush> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers>
所以整体来讲,主要是设置这两个内容属性值。
接下来,如果客户觉得,只是改变形状与外观,然而并没有什么卵用。因为没有实际意义上的动作,比如按下按键,需要实现编辑框内数据按一定算法进行运算,或者打印字符到相应的编辑框中。好吧,作为苦逼程序猿也只有默默加班了。我们将在下一章讲解如何添加事件处理---订阅事件。
End.
谢谢.