自定义「属性故事面板」与「属性设置动画」
This commit is contained in:
41
RazorEngineTest/PropertySetterAnimation.cs
Normal file
41
RazorEngineTest/PropertySetterAnimation.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace RazorEngineTest
|
||||
{
|
||||
public class PropertySetterAnimation : AnimationTimeline
|
||||
{
|
||||
// 定义依赖属性用于绑定目标值
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
DependencyProperty.Register("Value", typeof(object), typeof(PropertySetterAnimation));
|
||||
|
||||
public object Value
|
||||
{
|
||||
get { return GetValue(ValueProperty); }
|
||||
set { SetValue(ValueProperty, value); }
|
||||
}
|
||||
|
||||
// 指定目标属性类型(可设置为任意类型)
|
||||
public override Type TargetPropertyType => typeof(object);
|
||||
|
||||
// 创建动画实例
|
||||
protected override Freezable CreateInstanceCore() => new PropertySetterAnimation();
|
||||
|
||||
// 核心方法:返回固定值(无插值过程)
|
||||
public override object GetCurrentValue(
|
||||
object defaultOriginValue,
|
||||
object defaultDestinationValue,
|
||||
AnimationClock animationClock)
|
||||
{
|
||||
return Value; // 直接返回预设值
|
||||
}
|
||||
|
||||
// 可选:设置是否使用动画插值
|
||||
public bool IsAnimationEnabled { get; set; } = false;
|
||||
}
|
||||
}
|
||||
46
RazorEngineTest/PropertyStoryboard.cs
Normal file
46
RazorEngineTest/PropertyStoryboard.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace RazorEngineTest
|
||||
{
|
||||
public class PropertyStoryboard : Storyboard
|
||||
{
|
||||
private readonly SetterBaseCollection _setters = new SetterBaseCollection();
|
||||
|
||||
public SetterBaseCollection Setters => _setters;
|
||||
public new void Begin()
|
||||
{
|
||||
base.Begin();
|
||||
ApplyPropertySetters();
|
||||
}
|
||||
|
||||
private void ApplyPropertySetters()
|
||||
{
|
||||
foreach (SetterBase setterBase in _setters)
|
||||
{
|
||||
if (setterBase is Setter setter && GetTarget(setter.TargetName) is DependencyObject targetObject)
|
||||
{
|
||||
DependencyProperty property = setter.Property;
|
||||
object value = setter.Value;
|
||||
|
||||
if (property != null)
|
||||
{
|
||||
targetObject.SetValue(property, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 快捷添加Setter的方法
|
||||
public void AddSetter(DependencyProperty property, object value)
|
||||
{
|
||||
_setters.Add(new Setter(property, value));
|
||||
}
|
||||
public DependencyObject GetTarget(string targetName) => throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,8 @@
|
||||
<ControlTemplate TargetType="{x:Type local:StateAdornerDecorator}">
|
||||
<!--使用「装饰器」容器做为控件模板的根对象,保证当前控件可以为任何控件提供装饰层-->
|
||||
<AdornerDecorator>
|
||||
<Border BorderBrush="{TemplateBinding BorderBrush}"
|
||||
<Border x:Name="dtRoot"
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}">
|
||||
<Border.Background>
|
||||
<SolidColorBrush x:Name="BackgroundBrush" Color="White"/>
|
||||
@@ -50,6 +51,7 @@
|
||||
<VisualState x:Name="Selected">
|
||||
<Storyboard>
|
||||
<!--状态切换动画-->
|
||||
<local:PropertySetterAnimation Storyboard.TargetName="dtRoot" Storyboard.TargetProperty="Background" Value="Red" Duration="0:0:0"/>
|
||||
</Storyboard>
|
||||
</VisualState>
|
||||
<VisualState x:Name="NormalHover">
|
||||
|
||||
Reference in New Issue
Block a user