2009-07-24 107 views
0

我有一個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(); 
} 

當我把一個< StopStoryboard BeginStoryboardName = 「FadeOutStoryboard」/>它告訴我它無法找到FadeOutStoryboard。我對WPF編程相當陌生,因此可能有更好的方法來做到這一點,我對此持開放態度。 StopFadeOut()方法由父節點調用,該父節點創建我的usercontrol實例。

回答

1

將這些線構造。你只需要運行一次。

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

再經過他們的權利,加入這一行。

RegisterName("FadeOutStoryboard", FadeOutStoryboard);