WPF 堆栈面板可见性动画
我有一个带有按钮的堆栈面板,单击该按钮会使堆栈面板消失。我想对可见到隐藏的过渡形式进行动画处理,但一直无法做到。
我环顾四周,发现了一些看起来像这样的东西:
<StackPanel Margin="80,60,60,80" Background="Gray">
<StackPanel.Triggers >
<EventTrigger >
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetProperty="Visibility">
<DoubleAnimation Duration="0:0:5:0" From="Visible" To="Hidden"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</StackPanel.Triggers>
<Button Name="buttonTop" Content="TOP" Margin="40,40,40,40" Click="buttonTop_Click" Width="131" />
</StackPanel>
当然,它还没有 100% 实现。有什么想法吗?
I have a stackpanel with a button which, when clicked, makes the stackpanel disappear. I want to animate the transition form visible to hidden, but haven't been able to.
I looked around for a while and bumped into something that looks like this:
<StackPanel Margin="80,60,60,80" Background="Gray">
<StackPanel.Triggers >
<EventTrigger >
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard TargetProperty="Visibility">
<DoubleAnimation Duration="0:0:5:0" From="Visible" To="Hidden"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</StackPanel.Triggers>
<Button Name="buttonTop" Content="TOP" Margin="40,40,40,40" Click="buttonTop_Click" Width="131" />
</StackPanel>
which of course, is not 100% there yet. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
这几乎是情节提要中的设置器,其中 KeyTime 描述了应该设置值的时间。
因此,完整的故事板将如下所示:
编辑:如何在单击按钮时触发故事板:
You can use
This is pretty much a setter in a storyboard, where KeyTime describes the time when the value should be set.
So the full storyboard would be like this:
edit: How to make the storyboard trigger when button is clicked:
Visibiltiy
是一个离散值 - 它要么打开,要么关闭,因此动画仍然会导致突然消失而不是逐渐淡出。您可以将StackPanel
的Opacity
从 1 设置为 0,然后将Visibilty
设置为Hidden
(或折叠
)之后。Visibiltiy
is a discrete value - it's either on or off, so animating will still result in a sudden disappearance rather than a gradual fading out. You could instead animate theOpacity
of theStackPanel
from 1 to 0, and then animate theVisibilty
toHidden
(orCollapsed
) after that.