故事板完成了Style的活动?

时间:2012-02-11 00:00:06

标签: c# wpf xaml mvvm storyboard

我是Storyboard动画的新手,但我认为这可能是一个问题,我无法解决这个问题。不过我在这里试试运气,也许有些人知道如何帮助我。

我的场景:我想在我的应用程序中显示一个具有淡入淡出效果的弹出窗口。我也希望通过MVVM这样做,所以我的控制包装了fadein效果和弹出窗口不应该使用代码隐藏,我的应用程序应该只需要将此控件的datacontext重置为新的viewmodel以显示新消息。

我的问题是我无法确定动画何时结束,因为我需要在样式中设置fadein动画。

我的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>

任何想法如何在Storyboard完成后如何在我的ViewModel中收到通知?

1 个答案:

答案 0 :(得分:0)

您可以在故事板上处理已完成的事件。 文档在此处: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;
}

我认为这应该适用于您提供的代码。