一個很奇怪的問題,不知道大家遇到過沒有:
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);
}
已在c#技術區得到解決