Reputation: 403
I have a Windows TrayIcon application. When the user clicks on the tray icon, my WPF window gets shown. If the user closes the window it gets simply hidden. On the next click on the tray icon, the same window gets shown again.
The goal is now to have the appearance of the window animated. The window should "slide up" from the taskbar. And it should "slide down" when the user closes the window. I currently have a solution which I does not really like and I am wondering if there is better way. I have an animation which changes the height of the Layout Container (grid in this case):
<Storyboard x:Key="BottomUpSlideIn" Completed="BottomUpSlideIn_OnCompleted">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="grid">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="470"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
But it does actually not hide the window, it just sets its height to zero. Is it possible to set the visibility of the Window to hidden after the animation is done? Or do you know a different/better approach to get this done?
Upvotes: 0
Views: 2176
Reputation: 69959
I used this on one of my first applications... you may need to adjust the Trigger
to your requirements:
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard Name="SlideStoryboard">
<DoubleAnimation Name="SlideDoubleAnimation"
Storyboard.TargetName="SlideWindow" Storyboard.TargetProperty="Top"
Duration="0:0:0.75" Completed="SlideDoubleAnimation_Completed"
AccelerationRatio="0.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
SlideWindow
is the value of the Window.Name
property.
Then for the closing animation, I just adjusted this DoubleAnimation
object:
private void BeginCloseAnimation()
{
SlideDoubleAnimation.From = System.Windows.SystemParameters.WorkArea.Height -
this.Height - 2;
SlideDoubleAnimation.To = System.Windows.SystemParameters.WorkArea.Height;
SlideStoryboard.Begin();
close = true;
}
private void SlideDoubleAnimation_Completed(object sender, EventArgs e)
{
if (close) Close();
}
Upvotes: 1