基于WPF實(shí)現(xiàn)控件輪廓跑馬燈動(dòng)畫效果
代碼如下
一、創(chuàng)建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>
效果預(yù)覽

到此這篇關(guān)于基于WPF實(shí)現(xiàn)控件輪廓跑馬燈動(dòng)畫效果的文章就介紹到這了,更多相關(guān)WPF跑馬燈動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之順序表(SeqList)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了順序表的定義、原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
C#中倒計(jì)時(shí)功能的優(yōu)化方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了當(dāng)C#重復(fù)使用一段代碼倒計(jì)時(shí)時(shí),如何使用普通類和靜態(tài)方法,實(shí)現(xiàn)簡單的代碼封裝性、可擴(kuò)展性、可維護(hù)性,感興趣的可以了解下2024-01-01
C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換
這篇文章介紹了C#實(shí)現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的示例詳解
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)日期時(shí)間的格式化輸出的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-03-03
Windows系統(tǒng)中使用C#編寫藍(lán)牙通信程序的簡單實(shí)例
這篇文章主要介紹了Windows系統(tǒng)中使用C#編寫藍(lán)牙通信程序的簡單實(shí)例,文中的例子使用到了32feet.NET中的InTheHand.Net.Personal類庫,需要的朋友可以參考下2016-04-04

