一个很奇怪的问题,不知道大家遇到过没有:
1、在Canvas中直接设置元素位置,是有效果的,比如 Canvas.SetLeft(box, 50);
2、使用一个StoryBoard或直接BeginAnimation动画改变位置,也是有效果的 (见test1_Click)
3、在使用动画改变元素位置后,再使用Canvas.SetLeft,纹丝不动!!?
XAML代码:
<Canvas>
<Border x:Name="box" Width="100" Height="100" Background="Blue" Canvas.Left="150" Canvas.Top="90"/>
<Button x:Name="test1" Content="test1" Height="27" Canvas.Left="441" Canvas.Top="17" Width="57" Click="test1_Click"/>
<Button x:Name="test2" Content="test2" Height="27" Canvas.Left="441" Canvas.Top="57" Width="57" Click="test2_Click"/>
</Canvas>
后台代码:
private void test1_Click(object sender, RoutedEventArgs e)
{
//((Storyboard)this.Resources["move"]).Begin();
KeyTime time_layout = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200));
Storyboard sb = new Storyboard();
DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
sb.Children.Add(da);
Storyboard.SetTarget(da, box);
Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)"));
EasingDoubleKeyFrame kf_left = new EasingDoubleKeyFrame { KeyTime = time_layout, Value = 200 };
da.KeyFrames.Add(kf_left);
sb.Begin();
}
private void test2_Click(object sender, RoutedEventArgs e)
{
Canvas.SetLeft(box, 50);
}