Changing the visibility of the WPF viewport

I am trying to figure out how to animate a change from Visibile to Hidden for a WPF window. The way I'm currently working with the application is that the window is usually hidden, and when I move the mouse towards the screen, it appears, I use the logical visibility converter for this, but what I would like to do is so that the application accelerates more smoothly on the mouse, and then crawls back again.

I had nothing with animation, so I'm not sure how to do it. Firstly, I’m not quite sure what kind of animation I should use for this, secondly, I’m not sure whether I should run this in the IsWindowVisibile property in the viewmodel or if I have to bind it to the VisibilityChanged event, and thirdly, I'm not sure if this is possible by resizing the window.

[change]

If necessary, I will take an “opaque solution”, but this is not exactly the “sliding” effect that I am trying to get.

+4
source share
2 answers

I did something like this (opacity goes to 0 for 2 seconds and windows hide): just look at the code, just

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

  public void ShowMe() { (FindResource("showMe") as Storyboard).Begin(this); } public void HideMe() { (FindResource("hideMe") as Storyboard).Begin(this); } 

Just call HideMe() or ShowMe() instead of setting Visibility = Visibility.Hidden in the code

Edit

WPF is slow when moving windows, so if you need rolling animation:

  • Make a transparent window ( AllowsTransparency="True" Background="Transparent" WindowStyle="None" )

  • Put all your controls on an opaque panel ( Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}" )

  • Animate this Margin.Left panel from 0 to the ActualWidth window, then hide the window - this will fix the problem of saving the window size

+12
source

Playback with the Opacity / Visibility window requires a simple DoubleAnimation .

Example:

 IsVisibleChanged += new DependencyPropertyChangedEventHandler(MainWindow_IsVisibleChanged); void MainWindow_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) { DoubleAnimation da = new DoubleAnimation() { From = (IsVisible) ? 0 : 1, To = (IsVisible) ? 1 : 0, Duration = TimeSpan.FromSeconds(1) }; BeginAnimation(Window.OpacityProperty, da); } 

Problem:

To do this, as expected, you need to set AllowsTransparency to True in your window. If you do not set this, your window with Opacity set to 0 will be Black .

The problem is for this property to be True , you need to have WindowStyle as None . This means there is no frame around your window. This means no closure, minimization, maximization, recovery, header.

You must provide a custom template (possibly inheriting the Window class) to place these buttons there.

+2
source

Source: https://habr.com/ru/post/1332927/


All Articles