Doubleanimation不起作用,因为typ是一个整数。有任何Integeranimation类吗?或者以不同的方式做到这一点。 我对静态动画不感兴趣,但是事件驱动(最好是 程序化的动画。
public static void Swap(UIElement ui1, UIElement ui2, double seconds)
{
var z2 = (int)ui1.GetValue(Canvas.ZIndexProperty);
var z3 = (int)ui2.GetValue(Canvas.ZIndexProperty);
DoubleAnimation da1 = new DoubleAnimation();
DoubleAnimation da2 = new DoubleAnimation();
Duration d = new Duration(TimeSpan.FromSeconds(seconds));
da1.Duration = d;
da2.Duration = d;
Storyboard sb = new Storyboard();
Storyboard.SetTarget(da1, ui1);
Storyboard.SetTarget(da2, ui2);
Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.ZIndex)"));
Storyboard.SetTargetProperty(da2, new PropertyPath("(Canvas.ZIndex)"));
sb.Duration = d;
sb.Children.Add(da1);
sb.Children.Add(da2);
da1.To = (double)z3;
da2.To = (double)z2;
sb.Begin();
}
private void Btn1_Click_1(object sender, RoutedEventArgs e)
{
var z2 = (int)Image2.GetValue(Canvas.ZIndexProperty);
var z3 = (int)Image3.GetValue(Canvas.ZIndexProperty);
Swap(Image2, Image3, 3);
}
答案 0 :(得分:3)
您可以使用ObjectAnimationUsingKeyFrames和DiscreteObjectKeyFrame为离散值设置动画。
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Canvas.ZIndex)" Storyboard.TargetName="MyObject">
<DiscreteObjectKeyFrame KeyTime="0" Value="250"/>
</ObjectAnimationUsingKeyFrames>
或者您可以在代码中执行此操作。
答案 1 :(得分:1)
您无法为int
属性设置动画。想想它究竟意味着什么。
可能接近(视觉上)的是为不透明度设置动画。也许在动画完成事件中更改Zindex。