使用自定义位置的幻灯片动画打开/关闭弹出窗口

时间:2013-06-17 13:28:50

标签: c# wpf

我想从屏幕右侧打开Popup作为幻灯片动画,如下图所示 enter image description here

弹出窗口放在屏幕的右侧,现在关闭弹出窗口像往常一样打开没有任何动画。 请帮忙。 提前致谢

1 个答案:

答案 0 :(得分:3)

我用我的逻辑做到了,绝对不是一个完美的答案,但它是有效的。 我所做的是 1)将弹出/网格控件放在屏幕右侧,我必须用动画enter image description here打开/关闭弹出窗口

所以,当我想打开我的Popup时,我正在添加ThicknessAnimation,我的Popup / Grid动画到图像所示的部分(2),同时需要再次删除Popup / Grid我正在应用ThicknessAnimation以便移动我的弹出/网格到部分(1)如图所示 这是我的代码。

//for opening Popup
myPopupGrid.Margin = new Thickness(0, 0, -myPopupGrid.Width, 0);
ThicknessAnimation thicknessAnimation = new ThicknessAnimation();    
thicknessAnimation .From = myPopupGrid.Margin;    
thicknessAnimation .To = new Thickness(0, 0, 0, 0);
thicknessAnimation .Duration = new Duration(TimeSpan.FromSeconds(0.5));
myPopupGrid.BeginAnimation(Grid.MarginProperty, thicknessAnimation );
thicknessAnimation .Completed += delegate { myPopupGrid.Visibility = Visibility.Visible; }; 

//for closing Popup
myPopupGrid.Margin = new Thickness(0, 0, 0, 0);
ThicknessAnimation thicknessAnimation = new ThicknessAnimation();    
thicknessAnimation .From = myPopupGrid.Margin;    
thicknessAnimation .To = new Thickness(0, 0, -myPopupGrid.Width, 0);
thicknessAnimation .Duration = new Duration(TimeSpan.FromSeconds(0.5));
myPopupGrid.BeginAnimation(Grid.MarginProperty, thicknessAnimation );
thicknessAnimation .Completed += delegate { myPopupGrid.Visibility = Visibility.Hidden; }; 

//Here point of notice is I am changing Thickness in both cases
//From
new Thickness(0, 0, -myPopupGrid.Width, 0)
//To
new Thickness(0, 0, 0, 0);

反之亦然,这导致我的Popup重置其位置To and from,that it it it