2012-02-11 45 views
2

我是新來的故事板動畫,但我認爲這可能是一個問題,我不能解決簡單的方法。不過,我在這裏嘗試我的運氣,也許你們中的一些人知道如何幫助我。Storyboard完成Style的事件?

我的情景:我想在我的應用程序中顯示一個具有淡入淡出效果的彈出窗口。我也想通過MVVM做到這一點,所以我的控制包裝fadein效果和彈出窗口不應該使用代碼隱藏,我的應用程序應該只需要重置此控件的datacontext到一個新的viewmodel顯示一個新的消息。

我的問題是我無法確定動畫何時完成,因爲我需要在樣式中設置淡入淡出動畫。

我的XAML看起來像這樣:

<UserControl.Resources> 

    <Style x:Key="popupStyle" TargetType="{x:Type Border}" > 
     <Style.Triggers> 
      <Trigger Property="Visibility" Value="Visible"> 
       <Trigger.EnterActions> 
        <BeginStoryboard> 
         <Storyboard x:Name="FadingStoryBoard"> 
          <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.05" To="1" BeginTime="0:0:1" Duration="0:0:2.5" > 
           <DoubleAnimation.EasingFunction> 
            <ExponentialEase Exponent="5" EasingMode="EaseIn" /> 
           </DoubleAnimation.EasingFunction> 
          </DoubleAnimation> 
          <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0" BeginTime="0:0:6" Duration="0:0:8.5" > 
           <DoubleAnimation.EasingFunction> 
            <ExponentialEase Exponent="15" EasingMode="EaseOut" /> 
           </DoubleAnimation.EasingFunction> 
          </DoubleAnimation> 
         </Storyboard> 
        </BeginStoryboard> 
       </Trigger.EnterActions> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 

<Popup Name="Popup" IsOpen="{Binding IsVisible}" Height="{Binding PopupHeight}" Width="{Binding PopupWidth}" VerticalOffset="{Binding PopupVerticalOffset}" HorizontalOffset="{Binding PopupHorizontalOffset}" PopupAnimation="Fade" AllowsTransparency="True"> 
    <Border Style="{StaticResource popupStyle}" Name="PopupContent" Padding="1" BorderBrush="#000000" Background="AliceBlue" CornerRadius="5" BorderThickness="3,3,3,3"> 
     <!-- Events --> 
     <interact:Interaction.Triggers> 
      <interact:EventTrigger EventName="PreviewMouseDown"> 
       <cmd:EventToCommand Command="{Binding Path=PopupMouseDownCommand}" PassEventArgsToCommand="True" /> 
      </interact:EventTrigger> 
     </interact:Interaction.Triggers> 

     <DockPanel Name="ContentContainer" Background="Black" LastChildFill="True"> 
      <Image Source="{Binding MessageIcon}" DockPanel.Dock="Left" Margin="5,0,5,0" Width="32" Height="32" /> 
      <StackPanel Background="Transparent" DockPanel.Dock="Right" Margin="3"> 
       <TextBlock Name="PopupHeaderTextBlock" Margin="0,3,0,5" TextWrapping="Wrap" FontSize="10" Text="{Binding PopupHeaderText}" Foreground="White" Background="Transparent" /> 
       <TextBlock Name="PopupTextBlock" Text="{Binding PopupText}" TextWrapping="Wrap" FontSize="10" Foreground="White" Background="Transparent" /> 
      </StackPanel> 
     </DockPanel> 
    </Border> 
</Popup> 

任何人任何想法時,故事板完成我如何得到我的ViewModel的通知?

回答

0

您可以處理故事板上的Completed事件。 文檔在這裏:http://msdn.microsoft.com/en-us/library/system.windows.media.animation.timeline.completed.aspx

下面是從代碼隱藏附加事件的代碼:從構造函數中調用 :

private void AttachToCompletedEvent() 
{ 
    Style popupStyle = Resources["popupStyle"]; 
    TriggerBase trigger = popupStyle.Triggers[0]; 
    BeginStoryboard action = trigger.EnterActions[0] as BeginStoryboard; 
    Storyboard storyboard = action.Storyboard; 
    storyboard.Completed += CompletedEventHandler; 
} 

我覺得應該爲你提供的代碼工作。

+0

從樣式中不起作用 – KroaX 2012-02-11 00:20:36

+0

@KoraX您可以在後面的代碼中附加事件。我會更新我的答案來展示一個例子。 – JKor 2012-02-11 00:32:06

+0

正如我張貼我使用MVVM模式,並沒有從代碼隱藏的訪問故事板。我想過使用這個作爲解決方法,但我更喜歡來自XAML的解決方案 – KroaX 2012-02-11 08:44:57