-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedExpander.cs
74 lines (62 loc) · 2.88 KB
/
AnimatedExpander.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace BatteryMonitor
{
internal class AnimatedExpander : Expander
{
FrameworkElement expandContainer;
FrameworkElement expandSite;
TranslateTransform transform;
Duration animationTime = new Duration(TimeSpan.FromMilliseconds(400));
public AnimatedExpander()
{
Expanded += ExpanderHasExpanded;
Collapsed += ExpanderHasCollapsed;
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
expandContainer = GetTemplateChild("Part_ExpandContainer") as FrameworkElement;
expandSite = GetTemplateChild("Part_ExpandSite") as FrameworkElement;
transform = GetTemplateChild("Part_Transform") as TranslateTransform;
}
private void ExpanderHasExpanded(object sender, RoutedEventArgs args)
{
// we can't animate the height of expandContainer, the height isn't known yet
// even the inner children aren't calculated
expandContainer.Height = double.NaN;
expandSite.Visibility = Visibility.Visible;
var animation = new DoubleAnimation(-20, 0, animationTime, FillBehavior.Stop);
animation.EasingFunction = new CubicEase();
transform.BeginAnimation(TranslateTransform.YProperty, animation);
animation = new DoubleAnimation(0, 1, animationTime, FillBehavior.Stop);
animation.EasingFunction = new CubicEase();
expandSite.BeginAnimation(FrameworkElement.OpacityProperty, animation);
}
private void ExpanderHasCollapsed(object sender, RoutedEventArgs args)
{
var animation = new DoubleAnimation(0, -20, animationTime, FillBehavior.Stop);
var ease = new CubicEase();
ease.EasingMode = EasingMode.EaseOut;
animation.EasingFunction = ease;
animation.Completed += AnimationCollapsedCompleted;
transform.BeginAnimation(TranslateTransform.YProperty, animation);
animation = new DoubleAnimation(expandContainer.ActualHeight, 0, animationTime, FillBehavior.Stop);
ease = new CubicEase();
ease.EasingMode = EasingMode.EaseOut;
animation.EasingFunction = ease;
expandContainer.BeginAnimation(HeightProperty, animation);
animation = new DoubleAnimation(1, 0, animationTime, FillBehavior.Stop);
animation.EasingFunction = new CubicEase();
expandSite.BeginAnimation(FrameworkElement.OpacityProperty, animation);
}
private void AnimationCollapsedCompleted(object sender, EventArgs e)
{
expandSite.Visibility = Visibility.Collapsed;
expandContainer.Height = double.NaN;
}
}
}