title: How to Apply Storyboard as Style
body: <p>I want to apply a <code>Storyboard</code> to my <code>Rectangle</code> <code>Fill</code> like this:</p>
  
  <pre><code>&lt;Rectangle Name="MyRectangle"
    Width="100"
    Height="100"&gt;
    &lt;Rectangle.Fill&gt;
      &lt;SolidColorBrush x:Name="MySolidColorBrush" Color="Blue" /&gt;
    &lt;/Rectangle.Fill&gt;
    &lt;Rectangle.Triggers&gt;
      &lt;EventTrigger RoutedEvent="Rectangle.MouseEnter"&gt;
        &lt;BeginStoryboard&gt;
          &lt;Storyboard&gt;
            &lt;ColorAnimation 
              Storyboard.TargetName="MySolidColorBrush"
              Storyboard.TargetProperty="Color"
              From="Blue" To="Red" Duration="0:0:1" /&gt;  
          &lt;/Storyboard&gt;
        &lt;/BeginStoryboard&gt;
      &lt;/EventTrigger&gt;
    &lt;/Rectangle.Triggers&gt;
  &lt;/Rectangle&gt; 
  </code></pre>
  
  <p>But I want to insert the <code>Storyboard</code> in a <code>Style</code>, i tried this:</p>
  
  <pre><code>&lt;Style xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:s="clr-namespace:System;assembly=mscorlib" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     TargetType="{x:Type Rectangle}"&gt;
  
  &lt;Style.Triggers&gt;
  
      &lt;EventTrigger RoutedEvent="Shape.Loaded"&gt;
          &lt;BeginStoryboard&gt;
              &lt;Storyboard&gt;
                  &lt;ColorAnimation 
              Storyboard.TargetName="MySolidColorBrush"
              Storyboard.TargetProperty="Color"
              From="Blue" To="Red" Duration="0:0:1" /&gt;
              &lt;/Storyboard&gt;
          &lt;/BeginStoryboard&gt;
      &lt;/EventTrigger&gt;
  
  &lt;/Style.Triggers&gt;
  
  &lt;Setter Property="Shape.Fill" Value="Blue" x:Name="MySolidColorBrush"/&gt;
  
  &lt;/Style&gt;
  </code></pre>
  
  <p>Using this code:</p>
  
  <pre><code>var rect = new Rectangle();
  
  using (FileStream stream = new FileStream("myStyle.xaml", FileMode.Open))
     rect.Style = XamlReader.Load(stream) as Style;
  </code></pre>
  
  <p>But it does not work and throws an exception. How I have to change my Style?</p>