XAML:
<Window.Resources><Style x:Key="GridStyle1" TargetType="{x:Type Grid}"><Style.Resources><Storyboard x:Key="Storyboard1"><ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="{x:Null}"><EasingColorKeyFrame KeyTime="0:0:1" Value="Red"/></ColorAnimationUsingKeyFrames></Storyboard></Style.Resources><Setter Property="Background" Value="White"/></Style></Window.Resources><Grid x:Name="grid" Style="{DynamicResource GridStyle1}" MouseDown="Grid_MouseDown"/>
C# 1:
Style style = this.Resources["GridStyle1"] as Style; Storyboard storyboard = style.Resources["Storyboard1"] as Storyboard; storyboard.Begin();
Exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dllAdditional information: No target was specified for 'System.Windows.Media.Animation.ColorAnimationUsingKeyFrames'.
C# 2:
private void Grid_MouseDown(object sender, MouseButtonEventArgs e) { Style style = grid.Style; Storyboard storyboard = style.Resources["Storyboard1"] as Storyboard; storyboard.Begin(); }
Exception:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dllAdditional information: No target was specified for 'System.Windows.Media.Animation.ColorAnimationUsingKeyFrames'
Changing
storyboard.Begin();
to
storyboard.Begin(this);
results in no response of the app when pressed mouse down.
Note: The goal is to start a storyboard from code behind, not using triggers in XAML.