WPF动画窗口可见性变化 [英] WPF Animate Window Visibility Change
问题描述
我想弄清楚如何从动画中看得到变化隐藏WPF窗口。我目前拥有的应用程序的工作方式是,该窗口通常隐藏,当我把鼠标移动到它弹出屏幕的一侧,我使用一个布尔能见度转换器来做到这一点,但我想做些什么结束以及滑回原位,再后来让应用程序滑出更顺畅的鼠标。
I'm trying to figure out how to animate the change from Visibile to Hidden for a WPF window. The way I currently have the application working is that the window is normally hidden and when I move the mouse to the side of the screen it pops out, I'm using a boolean to visibility converter to do that but what I would like to do is to have the application slide out more smoothly on mouse over as well as slide back in again afterwards.
我没有不与任何动画,所以我不知道如何做到这一点。首先,我真的不知道我应该用做什么动画,其次我真的不知道我是否应该在视图模型触发此对IsWindowVisibile财产或者如果我应该把它绑定到VisibilityChanged事件;第三我不敢肯定这是可能的,当窗口大小是可变的。
I haven't don't anything with animations so I'm not sure how to do this. Firstly I'm not really sure what animation I should use the do this, secondly I'm not really sure whether I should trigger this on the "IsWindowVisibile" property in the viewmodel or if I should bind it to the VisibilityChanged event and thirdly I'm not sure if this is possible when the window size is variable.
如果有必要,我会采取不透明度的解决方案,但是这不完全是'滑'的效果,我试图让。
If necessary I will 'take' an opacity solution but that's not exactly the 'sliding' effect I am trying to get.
推荐答案
我没有类似的东西(不透明度在进行二秒钟后为0,窗口隐藏):光看code,它的简单
I did something like that (opacity goes to 0 during 2 seconds and window hides): just look at code, it's simple
MainWindow.xaml:
MainWindow.xaml:
<Storyboard x:Key="hideMe">
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:2" To="0.0"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Hidden}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="showMe">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:5" To="0.75"/>
</Storyboard>
MainWindow.xaml.cs
MainWindow.xaml.cs
public void ShowMe() {
(FindResource("showMe") as Storyboard).Begin(this);
}
public void HideMe() {
(FindResource("hideMe") as Storyboard).Begin(this);
}
就叫 HideMe()
或 SHOWME()
,而不是设置可见=能见度.Hidden在code
修改
移动窗口时WPF是缓慢的,所以如果你需要滑动动画:
WPF is slow when moving windows, so if you need sliding animation:
-
做一个透明的窗口(
AllowsTransparency =真的背景=透明WindowStyle =无
)
把所有的控件拖到不透明面板(背景={StaticResource的{X:静态SystemColors.ControlBrushKey}}
)
Put all your controls onto opaque panel (Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
)
动画此面板的 Margin.Left
从 0
来窗口的 ActualWidth的
,然后隐藏的窗口 - 这将删除保存窗口大小的问题
Animate this panel's Margin.Left
from 0
to window's ActualWidth
, then hide a window - this will remove a problem of saving window size
这篇关于WPF动画窗口可见性变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!