停止以编程方式实例化的故事板

时间:2009-07-24 14:27:48

标签: c# wpf xaml

我有一个EventTrigger会停止在我的XAML中定义的几个故事板......但现在我需要停止一个我以编程方式启动的故事板。

<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();
}

当我放一个&lt; StopStoryboard BeginStoryboardName =“FadeOutStoryboard”/&gt;它告诉我它找不到FadeOutStoryboard。我对WPF编程很新,所以可能有更好的方法来做到这一点,我对此持开放态度。父$调用StopFadeOut()方法,创建我的usercontrol实例。

1 个答案:

答案 0 :(得分:1)

将这些行移动到构造函数中。你只需要运行一次。

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

然后在它们之后添加此行。

RegisterName("FadeOutStoryboard", FadeOutStoryboard);