标签 标签 标签
- 一句话的事儿:
效果介绍
- 设计好的按钮可以被多次使用;
-
逻辑介绍
将设计好的按钮样式存放到Grid.Resource中;
- 设置模板属性;
- 调用模板,其中模板按钮中的Text采用重写的方式,即Text={TemplateBlind Button.Content}
源代码
按钮样式
<Grid.Resources>
<Style x:Key="beautifulBtn" TargetType="Button">
<Setter Property="Width" Value="150"/>
<Setter Property="Height" Value="50"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid RenderTransformOrigin="0.5,0.5">
<Grid.RenderTransform>
<RotateTransform Angle="0" x:Name="gridRotate"/>
</Grid.RenderTransform>
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="gridRotate" Storyboard.TargetProperty="Angle" To="360" Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<TextBlock x:Name="txtBlock" Text="{TemplateBinding Button.Content}" TextAlignment="Center" VerticalAlignment="Center" FontSize="25" Foreground="Blue"/>
<!--<Rectangle x:Name="rec" Width="100" Height="50" Fill="Violet" RadiusX="25" RadiusY="25" Opacity="0.5"></Rectangle>-->
<Rectangle x:Name="rec" Width="100" Height="50" RadiusX="25" RadiusY="25" Opacity="0.5">
<!--将填充部分单独写,为了后面单独调用做准备-->
<Rectangle.Fill>
<!--采用刷子来填充-->
<SolidColorBrush x:Name="recBrush" Color="Blue"/>
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="30" Duration="0:0:0.5"/>
<ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Gray" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="25" Duration="0:0:0.5"/>
<DoubleAnimation Storyboard.TargetName="txtBlock" Storyboard.TargetProperty="FontSize" To="25" Duration="0:0:0.5"/>
<ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
调用按钮
<Button Style="{StaticResource beautifulBtn}" Content="A按钮" Margin="321,184.5,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<Button Style="{StaticResource beautifulBtn}" Content="B按钮" Margin="321,255,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Style="{StaticResource beautifulBtn}" Content="C按钮" Margin="321,115,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<Button Style="{StaticResource beautifulBtn}" Content="D按钮" Margin="471,184.5,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<Button Style="{StaticResource beautifulBtn}" Content="E按钮" Margin="171,184.5,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
- 本文作者:GeekPower - Felix Sun
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!