随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

【转】[C#][WPF] 旋转的播放按钮

转自:http://www.dmskin.com

运行后播放按钮就会一直旋转,而暂停按钮不动。

复制代码
<Window x:Class="WPF.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF.Views"
        mc:Ignorable="d"Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <SolidColorBrush x:Key="ThemeColor" Color="#efefef" />
            <Geometry x:Key="Icon_Play">M995.76206814 481.44796294L614.67632587 261.43256005C609.21769525 258.2551482 603.1480239 256.66644227 596.99688042 256.66644227c-6.06967137 0-12.18007878 1.58870593-17.63870938 4.76611779C568.44090979 267.66517562 561.71946164 279.39715784 561.71946164 291.98459711l0 440.15301391c0 12.62817532 6.72144816 24.27868545 17.67944544 30.55203707 5.41789458 3.13667581 11.52830199 4.72538173 17.6387094 4.72538173 6.11040741 0 12.18007878-1.58870593 17.67944544-4.72538173l381.08574227-220.05613893C1006.72006543 536.27868545 1013.44151358 524.62817532 1013.44151358 512 1013.44151358 499.41256073 1006.72006543 487.76205059 995.76206814 481.44796294z</Geometry>
            <Geometry x:Key="Icon_Pause">M347.648 873.472c0 54.784-45.568 99.84-102.4 99.84s-102.4-44.544-102.4-99.84V151.04c0-54.784 45.568-99.84 102.4-99.84s102.4 44.544 102.4 99.84v722.432z m533.504 0c0 54.784-45.568 99.84-102.4 99.84s-102.4-44.544-102.4-99.84V151.04c0-54.784 45.568-99.84 102.4-99.84s102.4 44.544 102.4 99.84v722.432z</Geometry>

            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="14" />
            </Style>
            <Style TargetType="TextBox" x:Key="row1">
                <Setter Property="FontSize" Value="14" />
                <Setter Property="HorizontalContentAlignment" Value="Center" />
            </Style>
  
            <Style x:Key="MusicPlayButton" TargetType="ToggleButton">
                <Setter Property="Cursor" Value="Hand" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">                            
                            <Border
                                x:Name="bg"
                                Background="White"
                                CornerRadius="100"
                                UseLayoutRounding="True">
                                <Path
                                    x:Name="icon"
                                    Width="16"
                                    Height="16"
                                    Margin="5,0,0,0"
                                    Data="{StaticResource Icon_Play}"
                                    Fill="Green"
                                    Stretch="Uniform" />
                                <Border.RenderTransform>
                                    <RotateTransform x:Name="rotateTransform" Angle="0" CenterX="16" CenterY="16" />
                                </Border.RenderTransform>
                            </Border>
                            
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter TargetName="icon" Property="Margin" Value="5,0,0,0" />
                                    <Setter TargetName="icon" Property="Data" Value="{StaticResource Icon_Play}" />
                                </Trigger>
                                <Trigger Property="IsChecked" Value="False">
                                    <Setter TargetName="icon" Property="Margin" Value="0" />
                                    <Setter TargetName="icon" Property="Data" Value="{StaticResource Icon_Pause}" />
                                    <Setter TargetName="icon" Property="Fill" Value="Red" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="bg" Property="Background" Value="{StaticResource ThemeColor}" />
                                </Trigger>

                                <EventTrigger RoutedEvent="ToggleButton.Checked">
                                    <BeginStoryboard x:Name="Spinner">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="rotateTransform"
                                                             Storyboard.TargetProperty="Angle"
                                                             To="360"
                                                             RepeatBehavior="Forever"
                                                             Duration="0:0:10" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="ToggleButton.MouseEnter">
                                    <PauseStoryboard BeginStoryboardName="Spinner" />
                                </EventTrigger>
                                <EventTrigger RoutedEvent="ToggleButton.MouseLeave">
                                    <ResumeStoryboard BeginStoryboardName="Spinner" />
                                </EventTrigger>
                                <EventTrigger RoutedEvent="ToggleButton.Unchecked">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="rotateTransform"
                                                             Storyboard.TargetProperty="Angle"
                                                             To="0"
                                                             Duration="0:0:0.1" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="36" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <TextBlock Text="{Binding NowTimeText}" Margin="10 0" VerticalAlignment="Center" />

        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <ToggleButton Width="32" Height="32" Style="{StaticResource MusicPlayButton}" IsChecked="{Binding RunState}" />
        </StackPanel>

        <TextBox Margin="3" Grid.Row="1" AcceptsReturn="True" TextWrapping="Wrap" 
                 VerticalScrollBarVisibility="Auto" />
    </Grid>
</Window>
复制代码

 

posted on   z5337  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2023-09-05 [转][C#]泛型 T
2017-09-05 [转]IDEA 新建 JSP 项目时
2017-09-05 [转] AForge.NET 图像处理类
2013-09-05 自定义 TreeView 第三种状态(C#自定义控件)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示