2012-04-11 60 views
0

我有一個UniformGrid其中包含一堆按鈕,當UniformGrid首次顯示它時,通過動畫顯示每個按鈕來循環。這一切都可以正常工作,但是當用戶按下某個按鈕時,所有按鈕都將從網格中移除,並且一些新的按鈕將被創建並再次生成動畫。麻煩動畫在運行時創建按鈕

這是我如何在代碼中創建按鈕

int ModelsAnimateIndex = 0; // Index of button to animate 

    private void GetModels() 
    { 
     DirectoryInfo di = new DirectoryInfo(Globals.ModelsPath); 
     FileInfo[] fis = di.GetFiles(); 

     // ugModels is the UniformGrid 

     switch (fis.Length) 
     { 
      case 1: 
       ugModels.Rows = 1; 
       ugModels.Columns = 1; 
       break; 
      case 2: 
       ugModels.Rows = 1; 
       ugModels.Columns = 2; 
       break; 
      case 3: 
      case 4: 
       ugModels.Rows = 2; 
       ugModels.Columns = 2; 
       break; 
      case 5: 
      case 6: 
       ugModels.Rows = 2; 
       ugModels.Columns = 3; 
       break; 
      case 7: 
      case 8: 
      case 9: 
       ugModels.Rows = 3; 
       ugModels.Columns = 3; 
       break; 
      default: 
       break; 
     } 

     foreach (FileInfo s in fis) 
     { 
      ugModels.Children.Add(new Button() 
      { 
       Background = ThemeColour,        // SolidBrush 
       Foreground = new SolidColorBrush(Colors.White), 
       Name = "btn" + s.Name.Split('.')[0].Replace(" ",""), 
       Style = MainButtonStyle,        // Button Style 
       Content = s.Name.Split('.')[0] 
      }); 
     } 
    } 

,現在它開始動畫新創建的按鈕

private void btnModelSelect_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     // TODO: Add event handler implementation here. 
     sbShowMenuButton.Completed += new EventHandler(sbShowModels_Completed); 
     Storyboard.SetTargetName(sbShowMenuButton.Children[0], ((Button)ugModels.Children[ModelsAnimateIndex]).Name); 
     Storyboard.SetTargetName(sbShowMenuButton.Children[1], ((Button)ugModels.Children[ModelsAnimateIndex]).Name); 
     Storyboard.SetTargetName(sbShowMenuButton.Children[2], ((Button)ugModels.Children[ModelsAnimateIndex]).Name); 
     sbShowMenuButton.Begin((Button)ugModels.Children[ModelsAnimateIndex]); // Error is here 
    } 

當第一個按鈕嘗試動畫我得到的按鈕點擊事件以下錯誤

在'System.Windows.Controls.Button'的名稱範圍中找不到'btnTestModel'名稱。

+0

嘗試使用SetTarget代替SetTargetName的。無論如何你有一個對象的引用,那麼爲什麼讓WPF必須查找它? – 2012-04-11 14:01:47

+0

當更改爲SetTarget時,在開始時出現以下錯誤。 '[Unknown]'屬性不指向路徑'(0)。(1)[0]。(2)''中的DependencyObject。 – Gaz83 2012-04-11 14:29:53

回答

0

我已經解決了我的問題。我需要在像這樣創建的按鈕上創建RenderTransform。

foreach (FileInfo s in fis) 
     { 
      ugModels.Children.Add(new Button() 
      { 
       Background = ThemeColour, 
       //   This Fixed the problem   // 
       RenderTransform = new TransformGroup() 
       { 
        Children = new TransformCollection() 
        { 
         new ScaleTransform(), 
         new SkewTransform(), 
         new RotateTransform(), 
         new TranslateTransform() 
        } 
       },     
       //////////////////////////////////////////////// 
       BorderBrush = null, 
       Foreground = new SolidColorBrush(Colors.White), 
       Name = "btn" + s.Name.Split('.')[0].Replace(" ", ""), 
       Margin = new Thickness(12, 12, 12, 12), 
       FontSize = 48, 
       Style = MainButtonStyle, 
       Content = s.Name.Split('.')[0] 
      }); 
     }  

感謝馬特的sugestion,我還修改設置故事板

private void btnMenu_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     sbShowMenuButton.Completed += new EventHandler(sbShowModels_Completed); 
     Storyboard.SetTarget(sbShowMenuButton, ((Button)ugModels.Children[ModelsAnimateIndex]));    
     sbShowMenuButton.Begin(); 
    }