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.總結

 

 

通過此代碼,希望給初學者一些啟示,有更多想法的同學,多多批評,不吝賜教。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com