zoukankan      html  css  js  c++  java
  • WPF自定义控件之带倒计时的按钮--Button

    1.说明

    之前做过一个小项目,点击按钮,按钮进入倒计时无效状态,计时完成后,恢复原样,现在就实现该效果---带倒计时的按钮

    2.效果

    1)正常状态               2)MouseOver(只有背景色变化)         3)点击进入无效状态         

    4)在无效状态下计时              5)恢复正常状态

    3.XAML代码
     1 <!--冷却计时按钮样式-->
           <!--通过修改颜色值参数,以更改按钮颜色样式,更多修改,还请自行DIY-->
    <SolidColorBrush x:Key="ButtonForeground" Color="#FFF9FBFD"/>
    <SolidColorBrush x:Key="RactangleFill" Color="#FF6EB3F7"/>

    <SolidColorBrush x:Key="MouseOverFill" Color="#FF8CC3F9"/>
    <SolidColorBrush x:Key="PressedFill" Color="#888CC3F9"/>
    <SolidColorBrush x:Key="EnabledFill" Color="#FF6EB3F7"/>
    <SolidColorBrush x:Key="EnabledOpacityMask" Color="#99FFFFFF"/>

    <!--总样式-->

    2 <Style x:Key="TimerBtnStyle" TargetType="{x:Type Button}"> <Setter Property="FontSize" Value="14"/>
    73         <Setter Property="Foreground" Value="{StaticResource ButtonForeground}"/>
    3 <Setter Property="MinHeight" Value="25"/> 4 <Setter Property="Template"> 5 <Setter.Value> 6 <ControlTemplate TargetType="{x:Type Button}"> 7 <Grid> 8 <Rectangle x:Name="rectangle" Margin="0" StrokeThickness="0" Fill="{StaticResource RectangleFill}"/> 9 <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="Center" Margin="0" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/> 10 </Grid> 11 <ControlTemplate.Triggers> 12 <EventTrigger RoutedEvent="Button.Click"> 13 <BeginStoryboard> 14 <Storyboard FillBehavior="Stop"> 15 <StringAnimationUsingKeyFrames Storyboard.TargetProperty="(ContentPresenter.Content)" Storyboard.TargetName="contentPresenter"> 16 <DiscreteStringKeyFrame KeyTime="0" Value="已提交"/> 17 <DiscreteStringKeyFrame KeyTime="0:0:1" Value="等待(30)"/> 18 <DiscreteStringKeyFrame KeyTime="0:0:2" Value="等待(29)"/> 19 <DiscreteStringKeyFrame KeyTime="0:0:3" Value="等待(28)"/> 20 <DiscreteStringKeyFrame KeyTime="0:0:4" Value="等待(27)"/> 21 <DiscreteStringKeyFrame KeyTime="0:0:5" Value="等待(26)"/> 22 <DiscreteStringKeyFrame KeyTime="0:0:6" Value="等待(25)"/> 23 <DiscreteStringKeyFrame KeyTime="0:0:7" Value="等待(24)"/> 24 <DiscreteStringKeyFrame KeyTime="0:0:8" Value="等待(23)"/> 25 <DiscreteStringKeyFrame KeyTime="0:0:9" Value="等待(22)"/> 26 <DiscreteStringKeyFrame KeyTime="0:0:10" Value="等待(21)"/> 27 <DiscreteStringKeyFrame KeyTime="0:0:11" Value="等待(20)"/> 28 <DiscreteStringKeyFrame KeyTime="0:0:12" Value="等待(19)"/> 29 <DiscreteStringKeyFrame KeyTime="0:0:13" Value="等待(18)"/> 30 <DiscreteStringKeyFrame KeyTime="0:0:14" Value="等待(17)"/> 31 <DiscreteStringKeyFrame KeyTime="0:0:15" Value="等待(16)"/> 32 <DiscreteStringKeyFrame KeyTime="0:0:16" Value="等待(15)"/> 33 <DiscreteStringKeyFrame KeyTime="0:0:17" Value="等待(14)"/> 34 <DiscreteStringKeyFrame KeyTime="0:0:18" Value="等待(13)"/> 35 <DiscreteStringKeyFrame KeyTime="0:0:19" Value="等待(12)"/> 36 <DiscreteStringKeyFrame KeyTime="0:0:20" Value="等待(11)"/> 37 <DiscreteStringKeyFrame KeyTime="0:0:21" Value="等待(10)"/> 38 <DiscreteStringKeyFrame KeyTime="0:0:22" Value="等待(09)"/> 39 <DiscreteStringKeyFrame KeyTime="0:0:23" Value="等待(08)"/> 40 <DiscreteStringKeyFrame KeyTime="0:0:24" Value="等待(07)"/> 41 <DiscreteStringKeyFrame KeyTime="0:0:25" Value="等待(06)"/> 42 <DiscreteStringKeyFrame KeyTime="0:0:26" Value="等待(05)"/> 43 <DiscreteStringKeyFrame KeyTime="0:0:27" Value="等待(04)"/> 44 <DiscreteStringKeyFrame KeyTime="0:0:28" Value="等待(03)"/> 45 <DiscreteStringKeyFrame KeyTime="0:0:29" Value="等待(02)"/> 46 <DiscreteStringKeyFrame KeyTime="0:0:30" Value="等待(01)"/> 47 <DiscreteStringKeyFrame KeyTime="0:0:31" Value="等待(00)"/> 48 </StringAnimationUsingKeyFrames> 49 <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.IsEnabled)"> 50 <DiscreteBooleanKeyFrame KeyTime="0" Value="False"/> 51 <DiscreteBooleanKeyFrame KeyTime="0:0:32" Value="True"/> 52 </BooleanAnimationUsingKeyFrames> 53 </Storyboard> 54 </BeginStoryboard> 55 </EventTrigger> 56 <Trigger Property="IsFocused" Value="True"/> 57 <Trigger Property="IsDefaulted" Value="True"/> 58 <Trigger Property="IsMouseOver" Value="True"> 59 <Setter Property="Fill" TargetName="rectangle" Value="{StaticResource MouseOverFill}"/> 60 </Trigger> 61 <Trigger Property="IsPressed" Value="True"> 62 <Setter Property="Fill" TargetName="rectangle" Value="{StaticResource PressedFill}"/> 63 </Trigger> 64 <Trigger Property="IsEnabled" Value="False"> 65 <Setter Property="Fill" TargetName="rectangle" Value="{StaticResource EnabledFill}"/> 66 <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{StaticResource EnabledOpacityMask}"/> 67 </Trigger> 68 </ControlTemplate.Triggers> 69 </ControlTemplate> 70 </Setter.Value> 71 </Setter> 72 </Style>
    4.应用
    1 <Button Width="65" Content="提交请求" FontSize="14" Style="{StaticResource TimerBtnStyle}" />
    5.总结

    通过此代码,希望给初学者一些启示,有更多想法的同学,多多批评,不吝赐教。

  • 相关阅读:
    LeetCode --- Climbing Stairs
    LeetCode --- Best Time to Buy and Sell Stock II
    LeedCode --- Best Time to Buy and Sell Stock
    git命令总结
    LeetCode --- Jump Game II
    Hdu 4497
    数据库lib7第4题创建存储过程
    Hdu 4496
    Hdu 4493
    快速排序
  • 原文地址:https://www.cnblogs.com/smlusm/p/3234463.html
Copyright © 2011-2022 走看看