2012-06-04 82 views
0
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
x:Class="storyboard.MainWindow" 
x:Name="Window" 
Title="MainWindow" 
Width="640" Height="480"> 
<Window.Resources> 
    <Storyboard x:Key="all_in_one"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[3].(GradientStop.Color)" Storyboard.TargetName="btn_a"> 
      <EasingColorKeyFrame KeyTime="0" Value="#FFCDCDCD"/> 
      <EasingColorKeyFrame KeyTime="0:0:0.6" Value="#FFED0B00"/> 
      <EasingColorKeyFrame KeyTime="0:0:1" Value="#FFCDCDCD"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 
</Window.Resources> 


<Grid x:Name="LayoutRoot"> 
    <Button x:Name="btn_a" Content="A" HorizontalAlignment="Left" Height="56" Margin="117,134,0,0" VerticalAlignment="Top" Width="66" Click="btn_a_Click" /> 
    <Button x:Name="btn_b" Content="B" HorizontalAlignment="Left" Height="56" Margin="187,134,0,0" VerticalAlignment="Top" Width="66"/> 
    <Button x:Name="btn_c" Content="C" Height="56" Margin="257,134,301,0" VerticalAlignment="Top"/> 
</Grid> 

申請同一故事板

enter image description here

這裏我創建用於按鈕( 「btn_a」)情節串連圖板。

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     this.InitializeComponent(); 

     // Insert code required on object creation below this point. 
    } 

    private void btn_a_Click(object sender, RoutedEventArgs e) 
    { 
     Storyboard button_animation = (Storyboard)(FindResource("all_in_one")); 
     button_animation.Begin();    
    } 
} 

我想相同的故事板應用到每個其他按鈕,諸如btn_b和在代碼btn_c後面動態。

如果我點擊按鈕b,它必須動畫和按鈕c以及。

回答

0

這可能不是最好的辦法。話雖這麼說,你可以做這樣的:

private void btnC_Click(object sender, RoutedEventArgs e) 
    { 
     Storyboard storyboard = Resources["all_in_one"] as Storyboard; 
     Storyboard.SetTargetName(storyboard, "btnC"); 

     storyboard.Begin(); 
    } 

您需要從XAML中刪除這個工作你Storyboard.TargetName分配。

如果您需要一次啓動多個故事板,只需使用新的目標名稱調用Storyboard.SetTargetName函數後再調用Begin()。

+0

有沒有辦法在XAML中完全做到這一點?我猜測使用DataTrigger? –