首页 > 其他分享 >WPF 闪烁动画

WPF 闪烁动画

时间:2024-10-04 10:13:03浏览次数:1  
标签:动画 触发器 定义 IsAnimation WPF 闪烁 属性

实现

使用一个Boder控件,通过后台属性控制Boder的背景色变化,实现闪烁的效果。

1. xaml视图代码

<Border Width="15" Height="15" Margin="25 0 0 0" >
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Setter Property="Background" Value="#FF3BA245" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsAnimation}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard x:Name="stateAnimation">
                            <Storyboard AutoReverse="True" RepeatBehavior="Forever">
                                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Background.(SolidColorBrush.Color)">
                                    <EasingColorKeyFrame KeyTime="0" Value="Transparent" />
                                    <EasingColorKeyFrame KeyTime="00:00:0.3" Value="#00ff00" />
                                    <EasingColorKeyFrame KeyTime="00:00:0.6" Value="#ff0000" />
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <StopStoryboard BeginStoryboardName="stateAnimation" />
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>
  • <Style.Triggers> 定义了一系列触发器(Triggers),用于响应特定的数据变化。
  • DataTrigger监视与 IsAnimation 属性的绑定,当 IsAnimation 的值为 True 时,触发该触发器内定义的动画。
  • EnterActions: 当触发器条件为真时,进入动作会开始一个名为 stateAnimation 的动画故事板。
  • Storyboard 是动画的容器。这里设置为自动反向(AutoReverse="True")并且循环播放(RepeatBehavior="Forever")。
  • ColorAnimationUsingKeyFrames: 定义了一个色彩动画,目标是 Background 的颜色属性。
  • KeyFrames: 定义关键帧,第一帧为透明色,时间为0s时;第二帧为绿色,时间为0.3s时;第三帧为红色,时间为0.6s时。
  • ExitActions: 当 IsAnimation 的值变为 False 时,退出行为会停止名为 stateAnimation 的动画。

2. cs后台代码

定义一个名为IsAnimation的通知属性,属性变化时通知前台,控制动画的执行与停止。

标签:动画,触发器,定义,IsAnimation,WPF,闪烁,属性
From: https://www.cnblogs.com/zp1207/p/18446387

相关文章

  • WPF ListBox ListBoxItemTemplate display image automatically via System.Timers.Ti
    //xaml<Windowx:Class="WpfApp6.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......
  • WPF Datagrid display via DataGridTemplateColumn
    <DataGridTemplateColumnHeader="Image"><DataGridTemplateColumn.CellTemplate><DataTemplate><ImageSource="{BindingDataContext.ImgUrl,RelativeSource={RelativeSourceMode=F......
  • WPF FindResource,Resource[key] SystemColors TryFindResource
    //xaml<Windowx:Class="WpfApp3.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.micr......
  • WPF image via web url or uri
    Thebasicroadmapistodownloadwebimageatfirst,second convertitintomemeorystream,thirdassignthememorystreamtobitmapimageasStreamSource. //xaml<Windowx:Class="WpfApp2.MainWindow"xmlns="http://schemas.micro......
  • WPF Click Window show XY coordinates in MVVM via InvokeCommandAction of behavior
    1.Install Microsoft.Xaml.Behaviors.Wpf  2.<behavior:Interaction.Triggers><behavior:EventTriggerEventName="MouseDown"><behavior:InvokeCommandActionCommand="{BindingClickCommand}"......
  • WPF MVVM behavior CallMethodAction InvokeCommandAction
    1.Install  Microsoft.Xaml.Behaviors.Wpf 2.xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors"<behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMet......
  • 欢乐国庆:SciChart 8.5 for WPF Crack
    WPFChartPerformanceSciChart’slegendaryWPFChartperformanceisenabledbyourproprietary,in-house,C++crossplatform3Dgraphicsengine,VisualXccelerator.TheengineisentirelyhardwareacceleratedtargettingDirectXonWindowsplatform.Youw......
  • (六)WPF数据驱动模式
     WPF开发方式; MVVM(ModelViewViewModel)1.绑定XAML数据方式  在 XAML中添加绑定数据和绑定的操作属性        Content="{BindingMyVar}" 在XAML对应了的窗体类的构造函数添加数据绑定        this.DataContext=mainViewModel;//让此页面的数据取......
  • 我们用等距投影制作了一个动画视频
    一家国际网络安全公司委托我们制作一部关于其网络安全产品的解释性视频。为了有效传达产品的价值给潜在客户和利益相关者,我们决定采用等距投影技术制作动画视频。等距投影是一种复杂的视觉呈现方式,它能够让人物和物体看起来具有三维效果,而无需使用真正的3D图形。这种方法以其......
  • wpf ObservableCollection筛选功能
    viewmodel中定义原始数据及筛选后的数据,筛选后的数据类型为ICollectionView//原始数据列表publicObservableCollection<SchoolOutDto>SchoolList{get;set;}///<summary>///筛选数据后的列表///</summary>publicICollectionViewFilterSchoolList{get;set;}......