tags:

views:

262

answers:

1

I have an EventTrigger that will stop a few storyboards that are defined in my XAML ... but now i need to stop a storyboard that I'm starting programmatically.

<UserControl.Resources>
  <Storyboard x:Key="FadeIn"> ... Fade In Definition </StoryBoard>
  <Storyboard x:Key="FadeOut"> ... Fade In Definition </StoryBoard>
</UserControl>
<UserControl.Triggers>
  <EventTrigger RoutedEvent="Mouse.MouseLeave">
    <BeginStoryboard="{StaticResource FadeIn}" x:Name="FadeIn_BeginStoryboard" />
  </EventTrigger>
  <EventTrigger RoutedEvent="Mouse.MouseEnter">
    <StopStoryboard BeginStoryboardName="FadeIn_BeginStoryboard"/>
    <StopStoryboard BeginStoryboardName="FadeOut_BeginStoryboard"/>
  </EventTrigger>
</UserControl.Triggers>


Storyboard FadeOutStoryboard;
public void StopFadeOut() {
  FadeIn_BeginStoryboard.Storyboard.Stop();
  FadeOut_BeginStoryboard.Storyboard.Stop();
  FadeOutStoryboard = (Storyboard) FindResource("FadeOut");
  FadeOutStoryboard.Name="FadeOutStoryboard";
  FadeOutStoryboard.Begin();
}

When I put a < StopStoryboard BeginStoryboardName="FadeOutStoryboard" /> it tells me it can't find FadeOutStoryboard. I'm fairly new to WPF programming, so there might be a better way to do this and I'm open to that. The StopFadeOut() method is being invoked by a parent that creates an instance of my usercontrol.

+1  A: 

Move these lines to the constructor. You only need to run them once.

FadeOutStoryboard = (Storyboard)FindResource("FadeOut");
FadeOutStoryboard.Name = "FadeOutStoryboard";

Then add this line right after them.

RegisterName("FadeOutStoryboard", FadeOutStoryboard);
CannibalSmith