今天来讲解一下如何封**utton按钮的样式,首先这个一个普通的按钮和已经封装好的按钮。
我们可以在页面上直接定义按钮的样式,不过这次我们是封装样式。首先我们先建一个
资源字典
进去之后写取自己封装的样式
<Style x:Key="myButton" TargetType="{x:Type Button}">
<Setter Property="Width" Value="90"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Template">
<Setter.Value>
<!--重写控件:使用控件模板ControlTemplate-->
<ControlTemplate TargetType="Button">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3">
<!--小按钮的样式-->
<Border x:Name="border" Background="Red" BorderBrush="White" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3">
<Grid>
<TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2" Background="Transparent" Foreground="White"/>
<Border HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="shine" Width="Auto" CornerRadius="0,0,3,3">
<Border.Background>
<!--LinearGradientBrush(线性梯度画刷)-->
<LinearGradientBrush EndPoint="0.494,0.889" StartPoint="0.494,0.028">
<GradientStop Color="#99FFFFFF" Offset="0"/>
<GradientStop Color="#33FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
</Border>
<!--定义触发器:交互事件-->
<!--WPF trigger的主要类型有:Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种。
trigger主要运用的场景在Style、ControlTemplate、DataTemplate三个地方-->
<ControlTemplate.Triggers>
<!--UIElement.IsMouseOver属性:菜单项的“按下”状态-->
<Trigger Property="IsMouseOver" Value="True">
<!--TargetName指定操控的控件,Property控件属性,Value属性值-->
<Setter TargetName="shine" Property="Opacity" Value="0.6"/>
<Setter TargetName="border" Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="shine" Property="Opacity" Value="0.4"/>
<Setter TargetName="border" Property="Background" Value="#FFA4C0F0"/>
</Trigger>
</ControlTemplate.Triggers>
<!--定义触发器_End-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后在定义的button按钮进行按钮的绑定
我们可以在页面上直接定义按钮的样式,不过这次我们是封装样式。首先我们先建一个
资源字典
进去之后写取自己封装的样式
<Style x:Key="myButton" TargetType="{x:Type Button}">
<Setter Property="Width" Value="90"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Setter Property="Template">
<Setter.Value>
<!--重写控件:使用控件模板ControlTemplate-->
<ControlTemplate TargetType="Button">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3">
<!--小按钮的样式-->
<Border x:Name="border" Background="Red" BorderBrush="White" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3">
<Grid>
<TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2" Background="Transparent" Foreground="White"/>
<Border HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="shine" Width="Auto" CornerRadius="0,0,3,3">
<Border.Background>
<!--LinearGradientBrush(线性梯度画刷)-->
<LinearGradientBrush EndPoint="0.494,0.889" StartPoint="0.494,0.028">
<GradientStop Color="#99FFFFFF" Offset="0"/>
<GradientStop Color="#33FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
</Border>
<!--定义触发器:交互事件-->
<!--WPF trigger的主要类型有:Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种。
trigger主要运用的场景在Style、ControlTemplate、DataTemplate三个地方-->
<ControlTemplate.Triggers>
<!--UIElement.IsMouseOver属性:菜单项的“按下”状态-->
<Trigger Property="IsMouseOver" Value="True">
<!--TargetName指定操控的控件,Property控件属性,Value属性值-->
<Setter TargetName="shine" Property="Opacity" Value="0.6"/>
<Setter TargetName="border" Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="shine" Property="Opacity" Value="0.4"/>
<Setter TargetName="border" Property="Background" Value="#FFA4C0F0"/>
</Trigger>
</ControlTemplate.Triggers>
<!--定义触发器_End-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
然后在定义的button按钮进行按钮的绑定