2012-01-29 114 views
2

我做了一個小的WPF應用程序來滑動UserControls進出,我遇到的問題是UserControl動畫滑動時,然後單擊按鈕滑動另一個UserControl,它們都滑出 - 我是不知道這是爲什麼?故事板動畫

public UserControl1() 
{ 
    InitializeComponent(); 
} 

public void SlideIn(UserControl uc) 
{ 
    ThicknessAnimation tAnimation = new ThicknessAnimation(); 
    tAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5)); 
    tAnimation.From = new Thickness(500, 0, -500, 0); 
    tAnimation.To = new Thickness(0); 
    tAnimation.DecelerationRatio = 0.9; 

    Storyboard.SetTargetName(tAnimation, uc.Name); 
    Storyboard.SetTargetProperty(tAnimation, new PropertyPath(MarginProperty)); 

    Storyboard storyboard = new Storyboard(); 
    storyboard.Children.Add(tAnimation); 
    storyboard.Begin(uc); 
    this.Content = uc; 
} 

public void SlideOut(UserControl uc) 
{ 
    ThicknessAnimation tAnimation = new ThicknessAnimation(); 
    tAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5)); 
    tAnimation.To = new Thickness(-500, 0, 500, 0); 
    tAnimation.DecelerationRatio = 0.9; 

    Storyboard.SetTargetName(tAnimation, uc.Name); 
    Storyboard.SetTargetProperty(tAnimation, new PropertyPath(MarginProperty)); 

    Storyboard storyboard = new Storyboard(); 
    storyboard.Children.Add(tAnimation); 
    storyboard.Begin(this); 
} 

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    SlideOut(userControl1); 
    UserControl2 uc2 = new UserControl2(); 
    SlideIn(uc2); 
} 
+0

我試過'storyboard.Begin(uc);'但結果相同。 – Michael 2012-01-29 16:07:13

回答

1

您是否將UserControl的Name屬性設置在某處?或者更準確地說,你是否給第二個用戶控件一個不同於第一個的名稱?您的動畫通過設置爲

Storyboard.SetTargetName(tAnimation, uc.Name); 

因此名稱應該不同。您也可以致電

Storyboard.SetTarget(tAnimation, uc); 

指定目標。

+0

我在XAML中設置了Name屬性,兩個UserControl都有不同的名稱。我只是嘗試'Storyboard.SetTarget(tAnimation,uc);'但仍然是一樣的......如果有人會感興趣,我可以上傳這個應用程序,如果你想嘗試? – Michael 2012-01-29 16:48:12

+0

是的,繼續... – Clemens 2012-01-29 16:51:19

+0

我上傳到Dropbox的公共文件夾,應該可以被所有人訪問 - http://dl.dropbox.com/u/6727433/MyOwnLibrary.zip – Michael 2012-01-29 16:58:34