Wpf 自定义鼠标悬浮(Trigger) 控件缩放动画(ScaleTransform)RenderTransform图形旋转,缩放

此博客介绍了如何在WPF中利用样式和触发器为UI元素创建悬停时的缩放动画效果。通过设置`DockPanel`、`Border`和`TextBlock`的属性,并定义一个`Style`,当鼠标悬停在元素上时,元素会以平滑的动画方式放大1.2倍。这种效果可以增强用户体验并提升应用的视觉吸引力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上效果图,在需要使用的控件上给定

<DockPanel HorizontalAlignment="Left" Margin="0 32 0 0" Style="{StaticResource UIElementHover}">

  <Border Width="120" Height="120" CornerRadius="120" Style="{StaticResource UIElementHover}">

 <TextBlock FontSize="20" Text="DaskBoard" FontWeight="Bold" Style="{StaticResource UIElementHover}"></TextBlock>

  <Style x:Key="UIElementHover" TargetType="FrameworkElement">
                <Setter Property="RenderTransformOrigin" Value="0.5,0.5">
                </Setter>
                <Setter Property="Cursor" Value="Hand">
                    
                </Setter>
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <ScaleTransform   CenterX="0.5" CenterY="0.5" ScaleX="1" ScaleY="1"/>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" 
                                                 BeginTime="00:00:00" Duration="00:00:01"
                                                  To="1.2" 
                                                >
                                        <DoubleAnimation.EasingFunction>
                                            <PowerEase Power="1" EasingMode='EaseIn'></PowerEase>
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                    <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" 
                                                 BeginTime="00:00:00" Duration="00:00:01"
                                                To="1.2" >
                                        <DoubleAnimation.EasingFunction>
                                            <PowerEase Power="1" EasingMode='EaseIn'></PowerEase>
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)" 
                                                 BeginTime="00:00:00" Duration="00:00:01"
                                                >
                                        <DoubleAnimation.EasingFunction>
                                            <PowerEase Power="1" EasingMode='EaseOut'></PowerEase>
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                    <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)" 
                                                 BeginTime="00:00:00" Duration="00:00:01"
                                                >
                                        <DoubleAnimation.EasingFunction>
                                            <PowerEase Power="1" EasingMode='EaseInOut'></PowerEase>
                                        </DoubleAnimation.EasingFunction>
                                    </DoubleAnimation>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值