2010-08-20 39 views
0

我有以下問題需要解決:我的xaml中有一些省略號作爲按鈕工作,其中一些點擊時可能會打開兩個新按鈕。我把它們放在不同的畫布上,以這種方式生成的按鈕已經以不透明度0存在。我想要的是在轉換中單擊父按鈕時將該按鈕的不透明度設置爲1的效果。我怎樣才能做到這一點?如何基於Silverlight中的橢圓創建更改按鈕?

C#

 private void ExpandHarborButtons(object sender, MouseButtonEventArgs e) 
     { 
      Ellipse thisPath = (Ellipse)sender; 
      String test = (String)thisPath.DataContext; 
      for(int i = 0; i < DoubleHarbors.Children.Count; i++) 
      { 

       Ellipse button = (Ellipse)VisualTreeHelper.GetChild(DoubleHarbors, i); 

       if (test.Contains((String)button.DataContext)) 
       { 
        button.Opacity = 1; 
       } 
      } 
     } 

這就是我現在在做的方式,但我想這是行不通的。顯示的按鈕,但沒有我之前告訴的效果。

回答

4

創建一個DoubleAnimation並從點擊開始。事情是這樣的:

<Storyboard x:Name="fadeIn"> 
    <DoubleAnimation Storyboard.TargetName="ButtonName" From="0.0" To="0.1" Duration="0:0:0.5" 
       Soryboard.TargetProperty="Opacity"/> 
</Storyboard> 

然後在代碼:

fadeIn.Begin();

- 編輯 -

下面介紹如何在C#中做一個動畫。在XAML中定義它實際上更容易,但如果這真的是你想要的,這是一種方法。

 Storyboard sb = new Storyboard(); 
     DoubleAnimation da = new DoubleAnimation(); 
     da.From = 0; 
     da.To = 1.0; 
     da.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500)); 

     sb.Children.Add(da); 

     Storyboard.SetTarget(sb, sender as Button); 
     Storyboard.SetTargetProperty(Button1, new PropertyPath("Opacity")); 

     sb.Begin(); 
+0

我可以在cs文件中創建它嗎?按照你展示的方式,我將不得不創建一個按鈕的故事板? – 2010-08-20 19:53:50

+0

您可以在CS文件中創建動畫。從架構的角度來看,我不會這麼做,但從技術上講,它是有效的。我將編輯我的答案以顯示。 – Robaticus 2010-08-20 20:27:33