简体   繁体   English

WPF:使用 Storyboard 动画弹出不透明度

[英]WPF: Animating Popup opacity using Storyboard

I would like to animate a horizontal slide-in & fade combined with a Popup.我想动画一个水平滑入和淡入淡出结合弹出。 The slide is implemented using DoubleAnimation on HorizontalOffset and works fine with no fade animation.幻灯片是在HorizontalOffset ntalOffset 上使用DoubleAnimation实现的,并且工作正常,没有褪色 animation。 Once we add the "fade" animation via PopupAnimation="Fade" then the slide-out stops working.一旦我们通过PopupAnimation="Fade"添加“淡入淡出” animation 然后滑出停止工作。 I assume this is because "IsOpen" is now false and the popup has been removed (much the same way PopupAnimation="Slide" has no slide-out).我认为这是因为“IsOpen”现在为假并且弹出窗口已被删除(与PopupAnimation="Slide"没有滑出的方式非常相似)。

No problem, thinks I, I will just animate the Opacity manually, and animate "IsOpen" with a slight delay when closing.没问题,我想,我只会手动为不透明度设置动画,并在关闭时稍微延迟为“IsOpen”设置动画。

I'm aware that we can't animate Opacity directly, as it's the child elements opacity that needs to be affected.我知道我们不能直接为不透明度设置动画,因为需要影响子元素不透明度。

With this element:使用此元素:

                <local:AirspacePopup
                    x:Name="ControlsWindow"
                    ...
                    >
                    <Grid>
                        <Image Name="KeyMap" ... />
                    </Grid>
                    <local:AirspacePopup.Style>
                        <Style TargetType="local:AirspacePopup">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ShowViewControls}" Value="True">
                                    ...Animations go here

I've tried the following:我尝试了以下方法:

Binding directly to image - Has binding error直接绑定到图像 - 有绑定错误

<DoubleAnimation
    Storyboard.Target="{Binding ElementName=KeyMap}"
    Storyboard.TargetProperty="Opacity"
    From="0" To="1" Duration="0:0:0.30" />

Bunding to popup children (aka WPF Animate property of child without using Name ) - Has binding error绑定到弹出子项(又名WPF 不使用 Name 的子项的动画属性)- 有绑定错误

<DoubleAnimation
    Storyboard.Target="{Binding ElementName=ControlsWindow, Path=Children[0]}}"
    Storyboard.TargetProperty="Opacity"
    From="0" To="1" Duration="0:0:0.30" />

And creating a property in code-behind to forward the property to the children.并在代码隐藏中创建一个属性以将该属性转发给孩子。 No error, but breakpoint in C# is never hit没有错误,但 C# 中的断点从未被命中

<DoubleAnimation
    Storyboard.TargetProperty="ChildOpacity"
    From="0" To="1" Duration="0:0:0.30" />
        public double ChildOpacity
        {
            get => Child.Opacity;
            set {
                SetValue(ChildOpacityProperty, value);
                Child.Opacity = value;
            }
        }

I'm all outta ideas - what haven't I tried yet?我完全没有想法 - 我还没有尝试过什么?

NOTE: In case it's not clear, I'm very new (or rusty) in WPF注意:如果不清楚,我在 WPF 中非常新(或生锈)

I stumbled across an answer here: I can't animate a custom property in WPF我在这里偶然发现了一个答案: 我无法在 WPF 中为自定义属性设置动画

The property in the ViewModel -is- animated, just not via the accessors (?). ViewModel 中的属性是动画的,只是不是通过访问器 (?)。 Adding the OnPropertyChanged handler allows me to catch the change and react as appropriate添加OnPropertyChanged处理程序允许我捕捉更改并做出适当的反应

        public double ChildOpacity
        {
            get => Child.Opacity;
            set => SetValue(ChildOpacityProperty, value);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == ChildOpacityProperty)
            {
                Child.Opacity = (double)e.NewValue;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM