WPF 可见性改变时触发动画?
好吧,我有一个自定义控件,当 Visibility
更改为 Visible
时,我有一个带有进入/退出操作的 Trigger
但问题是,当退出操作会触发 Visibility
不再 Visible
,因此看不到动画,我该如何解决此问题?
这是我的触发器
:
<ControlTemplate.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource Hide}"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Show}"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
Well i have a custom control and when Visibility
is changed to Visible
I have a Trigger
with a enter/exit action but the problem is that when the exit action fires the Visibility
is no longer Visible
so the animation can't be seen how would I fix this?
here is my Trigger
:
<ControlTemplate.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource Hide}"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Show}"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也尝试过这个但失败了。我认为不可能在简单的
ControlTemplate
中使用Visibility
属性上的Trigger
来完成此任务。您可以做的是将Opacity
动画从 1 到 0 添加到不同属性的Trigger
,例如您添加的DependencyProperty
你自己背后的代码。I tried this too and failed. I think it is not possible to accomplish this in a simple
ControlTemplate
with aTrigger
on theVisibility
property. What you can do is add anOpacity
animation From 1 To 0 to aTrigger
for a different property, for instance aDependencyProperty
that you add in the code behind yourself.您还可以使用
ObjectAnimationUsingKeyFrames
来设置动画周期的Visibility
。在这种情况下,不需要任何代码隐藏。
You could also use
ObjectAnimationUsingKeyFrames
to setVisibility
for animation period.In such case there is no need in any codebehind.
有一种方法可以实现它。不是 100% 纯粹,但对我有用:
不要使用
Visibility
属性,而是使用Opacity
和Tag
属性。There is a way to achieve it. Not 100 % pure, but works for me:
Don't use
Visibility
property, but useOpacity
andTag
property.