动画化用户控件的可见性

时间:2013-03-27 11:45:56

标签: wpf animation user-controls visibility

我有一个用户控件,用于定义可视性隐藏的故事板。 show(Root是usercontrol中的顶级网格控件):

<Storyboard x:Key="VisibleStoryboard">
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Visibility">
        <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0" />
    </ObjectAnimationUsingKeyFrames>

    <DoubleAnimation Storyboard.TargetName="RootScale" Storyboard.TargetProperty="ScaleY" From="0" To="1" Duration="{StaticResource Duration}" />

    <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1" Duration="{StaticResource Duration}"/>
</Storyboard>

<Storyboard x:Key="CollapsedStoryboard">
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root" Storyboard.TargetProperty="Visibility">
        <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="{StaticResource DurationKeyTime}" />
    </ObjectAnimationUsingKeyFrames>

    <DoubleAnimation Storyboard.TargetName="RootScale" Storyboard.TargetProperty="ScaleY" From="1" To="0" Duration="{StaticResource Duration}" />

    <DoubleAnimation Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0" Duration="{StaticResource Duration}"/>
</Storyboard>

当控件的可见性发生变化时,我正在应用这些故事板:

static MyControl()
{
    VisibilityProperty.OverrideMetadata(typeof(Warning), new PropertyMetadata(VisibilityChanged));
}

private static void VisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var element = (FrameworkElement)d;

    switch ((Visibility)e.NewValue)
    {
        case Visibility.Visible:
            ((Storyboard)element.FindResource("VisibleStoryboard")).Begin(element);
            break;
        case Visibility.Collapsed:
            ((Storyboard)element.FindResource("CollapsedStoryboard")).Begin(element);
            break;
    }
}

可见的故事板工作正常。但是,折叠的故事板无法正常工作,因为用户控件可见性会立即设置为Collapsed,使其在故事板运行之前消失。

在故事板生效之前,如何延迟或覆盖用户控制可见性的实际设置?

0 个答案:

没有答案