Uri*_*son 2 wpf attached-properties wpf-animation
我试图找到一种通用方法来在每次更新 double 类型的属性时运行动画。
这必须是适用于所有双精度值的单一解决方案。这意味着我不想AttachedProperty为每个UIElement属性编写一个专有属性(一个为Opacity,然后另一个为Height)。
我想完成的一个伪示例:
<TextBlock x:Name="pageTitle" Text="title example" attached:AnimatedPropertyPath="(UIElement.Opacity)" Opacity="{Binding Path=TitleOpacity}" />
Run Code Online (Sandbox Code Playgroud)
附加属性应该侦听不透明度的任何变化,取消它并运行一个使其逐渐变化的动画。
我的问题:
我想避免使用 DataTriggers,因为它需要太多的 xaml。最好将其作为附加属性嵌入,就像上面的伪 xaml 一样。
我的问题:
- 这个确切的语法有意义吗?可行吗?
它必须是附属财产吗?您愿意使用行为吗?
- 有没有办法取消绑定的不透明度属性立即更改并运行动画?
也许有一些技巧(据我所知没有)。再说一遍,这是否绝对必须拦截并取消正常的 DP 操作?
- 任何示例链接将不胜感激,因为我自己找不到任何链接。
好吧,如果您可以稍微调整一下您的要求,我可以给您一个例子:
因此,如果您的要求是在任何双DP 绑定到值变化时为其设置动画,我们可以使用Behavior
public class AnimateBehavior : Behavior<UIElement> {
public static readonly DependencyProperty ToAnimateProperty =
DependencyProperty.Register("ToAnimate", typeof(DependencyProperty),
typeof(AnimateBehavior), new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(double),
typeof(AnimateBehavior),
new FrameworkPropertyMetadata(0.0d, FrameworkPropertyMetadataOptions.None, ValueChangedCallback));
public DependencyProperty ToAnimate {
get { return (DependencyProperty) GetValue(ToAnimateProperty); }
set { SetValue(ToAnimateProperty, value); }
}
public double Value {
get { return (double) GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var item = d as AnimateBehavior;
if (item == null || item.AssociatedObject == null) {
return;
}
var newAnimation = new DoubleAnimation((double) e.NewValue, new Duration(new TimeSpan(0, 0, 1)));
item.AssociatedObject.BeginAnimation(item.ToAnimate, newAnimation);
}
}
Run Code Online (Sandbox Code Playgroud)
现在在 xaml 中:
<TextBlock Text="Hello">
<i:Interaction.Behaviors>
<local:AnimateBehavior ToAnimate="{x:Static TextBlock.OpacityProperty}" Value="{Binding ValueYouWantToBindToOpacity}" />
</i:Interaction.Behaviors>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
现在,通过这种方法,您可以为该控件的任何具有 double 类型值的 DP 制作动画。喜欢Opacity,FontSize...
与您最初的要求的主要区别是我们不将 绑定Value到元素。相反,我们将其绑定到Behavior. 现在,当这种情况发生变化时,我们会在行为中检测到它,并通过AssociatedObject行为的属性,将动画应用到实际项目上。
我们还通过提供当值通过 DP 改变行为时动画的属性来满足您满足多个双 DP 类型的要求。
如果你想要更通用,你可以让接受Behavior动画的持续时间和类型,以使其更通用。
DP 识别属性的替代方法:
如果你绝对想传递“Opacity”而不是 DP,那么尝试这样的事情:
public static readonly DependencyProperty ToAnimateProperty =
DependencyProperty.Register("ToAnimate", typeof(PropertyPath),
typeof(AnimateBehavior), new FrameworkPropertyMetadata(null));
public PropertyPath ToAnimate
{
get { return (PropertyPath)GetValue(ToAnimateProperty); }
set { SetValue(ToAnimateProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
所以我们做了ToAnimate一个PropertyPath
并在ValueChanged函数中
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var item = d as AnimateBehavior;
if (item == null || item.AssociatedObject == null) {
return;
}
var sb = new Storyboard();
var newAnimation = new DoubleAnimation((double) e.NewValue, new Duration(new TimeSpan(0, 0, 1)));
Storyboard.SetTarget(newAnimation, item.AssociatedObject);
Storyboard.SetTargetProperty(newAnimation, item.ToAnimate);
sb.Children.Add(newAnimation);
sb.Begin();
}
Run Code Online (Sandbox Code Playgroud)
我们创建一个Storyboard并使用它,PropertyPath您可以拥有:
<TextBlock Text="Hello">
<i:Interaction.Behaviors>
<local:AnimateBehavior ToAnimate="Opacity" Value="{Binding ValueYouWantToBindToOpacity}" />
</i:Interaction.Behaviors>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
与此方法相比,我仍然更喜欢 DP。