文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

基于WPF实现控件轮廓跑马灯动画效果

2022-11-13 14:36

关注

代码如下

一、创建EdgeLight.xaml代码如下。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:WPFDevelopers.Controls">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Basic/ControlBasic.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style TargetType="{x:Type controls:EdgeLight}" BasedOn="{StaticResource ControlBasicStyle}">
        <Setter Property="BorderBrush" Value="{DynamicResource PrimaryNormalSolidColorBrush}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Padding" Value="20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:EdgeLight}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="EdgeLightStoryboard">
                            <DoubleAnimation Duration="00:00:0.5"
                                             To="1"
                                             Storyboard.TargetName="PART_Top"
                                             Storyboard.TargetProperty="ScaleX"/>
                            <DoubleAnimation Duration="00:00:0.5"
                                             BeginTime="00:00:0.5"
                                             To="1"
                                             Storyboard.TargetName="PART_Right"
                                             Storyboard.TargetProperty="ScaleY"/>
                            <DoubleAnimation Duration="00:00:.5"
                                             BeginTime="00:00:01"
                                             To="1"
                                             Storyboard.TargetName="PART_Bottom"
                                             Storyboard.TargetProperty="ScaleX"/>
                            <DoubleAnimation Duration="00:00:.5"
                                             BeginTime="00:00:01.5"
                                             To="1"
                                             Storyboard.TargetName="PART_Left"
                                             Storyboard.TargetProperty="ScaleY"/>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>
                        <DockPanel LastChildFill="False">
                            <Rectangle DockPanel.Dock="Top" Height="{TemplateBinding LineSize}" Fill="{TemplateBinding BorderBrush}">
                                <Rectangle.RenderTransform>
                                    <ScaleTransform x:Name="PART_Top" ScaleX="0"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                            <Rectangle DockPanel.Dock="Right" Width="{TemplateBinding LineSize}" Fill="{TemplateBinding BorderBrush}">
                                <Rectangle.RenderTransform>
                                    <ScaleTransform x:Name="PART_Right" ScaleY="0"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                            <Rectangle DockPanel.Dock="Bottom" Height="{TemplateBinding LineSize}" Fill="{TemplateBinding BorderBrush}"
                                   RenderTransformOrigin="1,1">
                                <Rectangle.RenderTransform>
                                    <ScaleTransform x:Name="PART_Bottom" ScaleX="0"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                            <Rectangle DockPanel.Dock="Left" Width="{TemplateBinding LineSize}" Fill="{TemplateBinding BorderBrush}"
                                   RenderTransformOrigin="1,1">
                                <Rectangle.RenderTransform>
                                    <ScaleTransform x:Name="PART_Left" ScaleY="0"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </DockPanel>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          Margin="{TemplateBinding Padding}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                   
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsAnimation" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="beginAnimation"
                                                 Storyboard="{StaticResource EdgeLightStoryboard}" />
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="beginAnimation" />
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

二、EdgeLight.cs代码如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WPFDevelopers.Controls
{
    public class EdgeLight:ContentControl
    {
        public bool IsAnimation
        {
            get { return (bool)GetValue(IsAnimationProperty); }
            set { SetValue(IsAnimationProperty, value); }
        }

        public static readonly DependencyProperty IsAnimationProperty =
            DependencyProperty.Register("IsAnimation", typeof(bool), typeof(EdgeLight), new PropertyMetadata(false));



        public double LineSize
        {
            get { return (double)GetValue(LineSizeProperty); }
            set { SetValue(LineSizeProperty, value); }
        }

        public static readonly DependencyProperty LineSizeProperty =
            DependencyProperty.Register("LineSize", typeof(double), typeof(EdgeLight), new PropertyMetadata(1.0d));


        static EdgeLight()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(EdgeLight), new FrameworkPropertyMetadata(typeof(EdgeLight)));
        }

       

    }
}

三、新建EdgeLightExample.cs代码如下。

<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.EdgeLightExample"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
             xmlns:wpfdev="https://github.com/yanjinhuagood/WPFDevelopers"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <UniformGrid Columns="2">
            <wpfdev:EdgeLight IsAnimation="{Binding ElementName=myCheckBox,Path=IsChecked}" Margin="10" LineSize="2">
                <CheckBox Content="EdgeLight" x:Name="myCheckBox"/>
            </wpfdev:EdgeLight>
            <wpfdev:EdgeLight IsAnimation="{Binding ElementName=myToggleButton,Path=IsChecked}" Margin="10" 
                              BorderBrush="OrangeRed" LineSize="4">
                <ToggleButton Content="EdgeLight2" x:Name="myToggleButton"/>
            </wpfdev:EdgeLight>
        </UniformGrid>
    </Grid>
</UserControl>

效果预览

到此这篇关于基于WPF实现控件轮廓跑马灯动画效果的文章就介绍到这了,更多相关WPF跑马灯动画内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯