使用ToggleButton和StackPanel+Border实现圆角开关按钮动画效果

本文介绍了一个使用C#和WPF实现的按钮切换动画效果,包括按钮的宽度变化和背景图像的切换,通过代码展示了如何利用动画实现按钮的动态变化。
<ToggleButton  Height="40"  Width="105" HorizontalAlignment="Left" Margin="138,122,0,0" Name="button1" VerticalAlignment="Top" Click="button1_Click">
            <ToggleButton.Content>
                <StackPanel Name="s1" Width="100" Height="22" Orientation="Horizontal" HorizontalAlignment="Left">
                    <Border Name="cd1" CornerRadius="10" BorderThickness="1" Width="100" BorderBrush="#FF001900">
                        <Border.Background>
                            <ImageBrush ImageSource="/MoveButton;component/Images/12.png" />
                        </Border.Background>
                    </Border>
                    <Border Name="cd2" CornerRadius="10" BorderThickness="1" Width="100" BorderBrush="#FF001900">
                        <Border.Background>
                            <ImageBrush ImageSource="/MoveButton;component/Images/13.png" />
                        </Border.Background>
                    </Border>
                </StackPanel>
            </ToggleButton.Content>
        </ToggleButton>
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (button1.IsChecked == true)
            {
                DoubleAnimation d1 = new DoubleAnimation();
                d1.From = 0;
                d1.To = 100;
                d1.Duration = new Duration(TimeSpan.Parse("0:0:0.2"));
                cd2.BeginAnimation(StackPanel.WidthProperty, d1);

                DoubleAnimation d2 = new DoubleAnimation();
                d2.From = 100;
                d2.To = 0;
                d2.Duration = new Duration(TimeSpan.Parse("0:0:0.2"));
                cd1.BeginAnimation(StackPanel.WidthProperty, d2);
            }

            if (button1.IsChecked == false)
            {
                DoubleAnimation d1 = new DoubleAnimation();
                d1.From = 100;
                d1.To = 0;
                d1.Duration = new Duration(TimeSpan.Parse("0:0:0.2"));
                cd2.BeginAnimation(StackPanel.WidthProperty, d1);

                DoubleAnimation d2 = new DoubleAnimation();
                d2.From = 0;
                d2.To = 100;
                d2.Duration = new Duration(TimeSpan.Parse("0:0:0.2"));
                cd1.BeginAnimation(StackPanel.WidthProperty, d2);
            }
        }

代码没精简,无图无真相,看看OFF和ON

转载于:https://www.cnblogs.com/Events/p/3342447.html

<Style x:Key="RoundedComboBoxStyle" TargetType="ComboBox"> <Setter Property="Padding" Value="6,2"/> <Setter Property="BorderBrush" Value="LightGray"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Background" Value="White"/> <Setter Property="MinWidth" Value="80"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ComboBox"> <Grid> <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5" SnapsToDevicePixels="True"> <Grid> <ContentPresenter x:Name="ContentSite" Margin="6,2,30,2" VerticalAlignment="Center" HorizontalAlignment="Left" RecognizesAccessKey="True" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/> <ToggleButton x:Name="DropDownToggle" Focusable="False" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" Background="Transparent" BorderThickness="0" BorderBrush="Transparent" Width="30" Height="Auto" HorizontalAlignment="Right" VerticalAlignment="Center"> <Grid> <Path x:Name="Arrow" HorizontalAlignment="Center" VerticalAlignment="Center" Fill="Gray" Stroke="Transparent" StrokeThickness="0" Data="M 0 0 L 6 6 L 12 0 Z" Width="12" Height="6"/> </Grid> </ToggleButton> </Grid> </Border> <Popup x:Name="PART_Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide" MinWidth="{TemplateBinding ActualWidth}"> <Border Background="White" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="10"> <ScrollViewer> <StackPanel IsItemsHost="True"/> </ScrollViewer> </Border> </Popup> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="Border" Property="Background" Value="#FFF5F5F5"/> <Setter TargetName="Border" Property="BorderBrush" Value="#FFD9D9D9"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Border" Property="BorderBrush" Value="#FF7FADEA"/> </Trigger> <Trigger Property="IsFocused" Value="True"> <Setter TargetName="Border" Property="BorderBrush" Value="#FF3C8CE9"/> <Setter TargetName="Border" Property="BorderThickness" Value="1.5"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 分析
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值