title: Animating the visibility of a usercontrol
body: <p>I've got a usercontrol that defines storyboards for visibility hide &amp; show (Root is the top-level grid control in the usercontrol):</p>
  
  <pre><code>&lt;Storyboard x:Key="VisibleStoryboard"&gt;
      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Visibility"&gt;
          &lt;DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0" /&gt;
      &lt;/ObjectAnimationUsingKeyFrames&gt;
  
      &lt;DoubleAnimation Storyboard.TargetName="RootScale" Storyboard.TargetProperty="ScaleY" From="0" To="1" Duration="{StaticResource Duration}" /&gt;
  
      &lt;DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="{StaticResource Duration}"/&gt;
  &lt;/Storyboard&gt;
  
  &lt;Storyboard x:Key="CollapsedStoryboard"&gt;
      &lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Visibility"&gt;
          &lt;DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="{StaticResource DurationKeyTime}" /&gt;
      &lt;/ObjectAnimationUsingKeyFrames&gt;
  
      &lt;DoubleAnimation Storyboard.TargetName="RootScale" Storyboard.TargetProperty="ScaleY" From="1" To="0" Duration="{StaticResource Duration}" /&gt;
  
      &lt;DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0" Duration="{StaticResource Duration}"/&gt;
  &lt;/Storyboard&gt;
  </code></pre>
  
  <p>I'm applying these storyboards when the visibility of the control changes:</p>
  
  <pre><code>static MyControl()
  {
      VisibilityProperty.OverrideMetadata(typeof(Warning), new PropertyMetadata(VisibilityChanged));
  }
  
  private static void VisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
      var element = (FrameworkElement)d;
  
      switch ((Visibility)e.NewValue)
      {
          case Visibility.Visible:
              ((Storyboard)element.FindResource("VisibleStoryboard")).Begin(element);
              break;
          case Visibility.Collapsed:
              ((Storyboard)element.FindResource("CollapsedStoryboard")).Begin(element);
              break;
      }
  }
  </code></pre>
  
  <p>The visible storyboard works fine. However, the collapsed storyboard isn't working, as the user control visibility is immediately being set to Collapsed, making it disappear before the storyboard can run.</p>
  
  <p>How can I delay or override actually settings the user control visibility until after the storyboard has taken effect?</p>