locked
Trigger a Storyboard from C#? RRS feed

  • Question

  • I have a storyboard in <Window.Resources> declared in XAML and triggering nicely from a declared routed event.

    How do I begin hte storyboard from C#?
    I'm wrestling with FindResource() and the key name of the resource to get a reference to the storyboard, but not with success.

    Any links or snippets to working code will be greatly appreciated.

     

    Thank you.

    Thursday, February 25, 2010 10:11 PM

All replies

  • Here you go, but please remember this has no error checking, and should not be used as is :)

    XAML:
    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="WpfApplication1.MainWindow"
    	x:Name="Window"
    	Title="MainWindow"
    	Width="640" Height="480">
    	<Window.Resources>
    		<Storyboard x:Key="Storyboard1">
    			<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
    				<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    				<SplineDoubleKeyFrame KeyTime="00:00:01.9000000" Value="173"/>
    			</DoubleAnimationUsingKeyFrames>
    			<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
    				<SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
    				<SplineDoubleKeyFrame KeyTime="00:00:01.9000000" Value="140"/>
    			</DoubleAnimationUsingKeyFrames>
    		</Storyboard>
    	</Window.Resources>
    	<Window.Triggers>
    		<EventTrigger RoutedEvent="FrameworkElement.Loaded">
    			<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
    		</EventTrigger>
    	</Window.Triggers>
    
    	<Grid x:Name="LayoutRoot">
    		<Button x:Name="button" HorizontalAlignment="Left" Margin="84,76,0,0" VerticalAlignment="Top" Width="76" Height="39" Content="Button" RenderTransformOrigin="0.5,0.5">
    			<Button.RenderTransform>
    				<TransformGroup>
    					<ScaleTransform/>
    					<SkewTransform/>
    					<RotateTransform/>
    					<TranslateTransform/>
    				</TransformGroup>
    			</Button.RenderTransform>
    		</Button>
    	</Grid>
    </Window>
    C#
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
    	/// <summary>
    	/// Interaction logic for MainWindow.xaml
    	/// </summary>
    	public partial class MainWindow : Window
    	{
    		public MainWindow()
    		{
    			this.InitializeComponent();
    
    			// Insert code required on object creation below this point.
    			this.Loaded += new System.Windows.RoutedEventHandler(MainWindow_Loaded);
    		}
    
    		private void MainWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
    		{
                var sb = this.Resources["Storyboard1"] as System.Windows.Media.Animation.Storyboard;
                sb.Begin();
    		}
    	}
    }
    Friday, February 26, 2010 2:22 PM
    Moderator