1
1
using System ;
2
2
using System . Windows ;
3
+ using System . Windows . Controls ;
4
+ using System . Windows . Controls . Primitives ;
3
5
using System . Windows . Media ;
4
6
using System . Windows . Media . Animation ;
5
7
using System . Windows . Media . Effects ;
6
8
7
9
namespace MaterialDesignThemes . Wpf
8
10
{
11
+
9
12
public enum ShadowDepth
10
13
{
11
14
Depth0 ,
@@ -37,8 +40,10 @@ public ShadowLocalInfo(double standardOpacity)
37
40
public double StandardOpacity { get ; }
38
41
}
39
42
40
- public static class ShadowAssist
43
+ public class ShadowAssist
41
44
{
45
+
46
+ #region AttachedProperty : ShadowDepthProperty
42
47
public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty . RegisterAttached (
43
48
"ShadowDepth" , typeof ( ShadowDepth ) , typeof ( ShadowAssist ) , new FrameworkPropertyMetadata ( default ( ShadowDepth ) , FrameworkPropertyMetadataOptions . AffectsRender ) ) ;
44
49
@@ -51,7 +56,9 @@ public static ShadowDepth GetShadowDepth(DependencyObject element)
51
56
{
52
57
return ( ShadowDepth ) element . GetValue ( ShadowDepthProperty ) ;
53
58
}
59
+ #endregion
54
60
61
+ #region AttachedProperty : LocalInfoPropertyKey
55
62
private static readonly DependencyPropertyKey LocalInfoPropertyKey = DependencyProperty . RegisterAttachedReadOnly (
56
63
"LocalInfo" , typeof ( ShadowLocalInfo ) , typeof ( ShadowAssist ) , new PropertyMetadata ( default ( ShadowLocalInfo ) ) ) ;
57
64
@@ -60,24 +67,35 @@ private static void SetLocalInfo(DependencyObject element, ShadowLocalInfo? valu
60
67
61
68
private static ShadowLocalInfo ? GetLocalInfo ( DependencyObject element )
62
69
=> ( ShadowLocalInfo ? ) element . GetValue ( LocalInfoPropertyKey . DependencyProperty ) ;
70
+ #endregion
63
71
72
+ #region AttachedProperty : DarkenProperty
64
73
public static readonly DependencyProperty DarkenProperty = DependencyProperty . RegisterAttached (
65
74
"Darken" , typeof ( bool ) , typeof ( ShadowAssist ) , new FrameworkPropertyMetadata ( default ( bool ) , FrameworkPropertyMetadataOptions . AffectsRender , DarkenPropertyChangedCallback ) ) ;
66
75
67
76
private static void DarkenPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
68
77
{
78
+
69
79
var uiElement = dependencyObject as UIElement ;
70
80
var dropShadowEffect = uiElement ? . Effect as DropShadowEffect ;
71
81
82
+
72
83
if ( dropShadowEffect == null ) return ;
73
84
74
85
if ( ( bool ) dependencyPropertyChangedEventArgs . NewValue )
75
86
{
76
87
SetLocalInfo ( dependencyObject , new ShadowLocalInfo ( dropShadowEffect . Opacity ) ) ;
77
88
78
- var doubleAnimation = new DoubleAnimation ( 1 , new Duration ( TimeSpan . FromMilliseconds ( 350 ) ) )
89
+ TimeSpan time = GetShadowAnimationDuration ( dependencyObject ) ;
90
+
91
+ var doubleAnimation = new DoubleAnimation ( )
79
92
{
80
- FillBehavior = FillBehavior . HoldEnd
93
+ To = 1 ,
94
+ Duration = new Duration ( time ) ,
95
+ FillBehavior = FillBehavior . HoldEnd ,
96
+ EasingFunction = new CubicEase ( ) ,
97
+ AccelerationRatio = 0.4 ,
98
+ DecelerationRatio = 0.2
81
99
} ;
82
100
dropShadowEffect . BeginAnimation ( DropShadowEffect . OpacityProperty , doubleAnimation ) ;
83
101
}
@@ -86,9 +104,16 @@ private static void DarkenPropertyChangedCallback(DependencyObject dependencyObj
86
104
var shadowLocalInfo = GetLocalInfo ( dependencyObject ) ;
87
105
if ( shadowLocalInfo == null ) return ;
88
106
89
- var doubleAnimation = new DoubleAnimation ( shadowLocalInfo . StandardOpacity , new Duration ( TimeSpan . FromMilliseconds ( 350 ) ) )
107
+ TimeSpan time = GetShadowAnimationDuration ( dependencyObject ) ;
108
+
109
+ var doubleAnimation = new DoubleAnimation ( )
90
110
{
91
- FillBehavior = FillBehavior . HoldEnd
111
+ To = shadowLocalInfo . StandardOpacity ,
112
+ Duration = new Duration ( time ) ,
113
+ FillBehavior = FillBehavior . HoldEnd ,
114
+ EasingFunction = new CubicEase ( ) ,
115
+ AccelerationRatio = 0.4 ,
116
+ DecelerationRatio = 0.2
92
117
} ;
93
118
dropShadowEffect . BeginAnimation ( DropShadowEffect . OpacityProperty , doubleAnimation ) ;
94
119
}
@@ -103,7 +128,9 @@ public static bool GetDarken(DependencyObject element)
103
128
{
104
129
return ( bool ) element . GetValue ( DarkenProperty ) ;
105
130
}
131
+ #endregion
106
132
133
+ #region AttachedProperty : CacheModeProperty
107
134
public static readonly DependencyProperty CacheModeProperty = DependencyProperty . RegisterAttached (
108
135
"CacheMode" , typeof ( CacheMode ) , typeof ( ShadowAssist ) , new FrameworkPropertyMetadata ( new BitmapCache { EnableClearType = true , SnapsToDevicePixels = true } , FrameworkPropertyMetadataOptions . Inherits ) ) ;
109
136
@@ -116,7 +143,9 @@ public static CacheMode GetCacheMode(DependencyObject element)
116
143
{
117
144
return ( CacheMode ) element . GetValue ( CacheModeProperty ) ;
118
145
}
146
+ #endregion
119
147
148
+ #region AttachedProperty : ShadowEdgesProperty
120
149
public static readonly DependencyProperty ShadowEdgesProperty = DependencyProperty . RegisterAttached (
121
150
"ShadowEdges" , typeof ( ShadowEdges ) , typeof ( ShadowAssist ) , new PropertyMetadata ( ShadowEdges . All ) ) ;
122
151
@@ -129,5 +158,21 @@ public static ShadowEdges GetShadowEdges(DependencyObject element)
129
158
{
130
159
return ( ShadowEdges ) element . GetValue ( ShadowEdgesProperty ) ;
131
160
}
161
+ #endregion
162
+
163
+ #region AttachedProperty : ShadowAnimationDurationProperty
164
+ public static readonly DependencyProperty ShadowAnimationDurationProperty =
165
+ DependencyProperty . RegisterAttached (
166
+ name : "ShadowAnimationDuration" ,
167
+ propertyType : typeof ( TimeSpan ) ,
168
+ ownerType : typeof ( ShadowAssist ) ,
169
+ defaultMetadata : new FrameworkPropertyMetadata (
170
+ defaultValue : new TimeSpan ( 0 , 0 , 0 , 0 , 180 ) ,
171
+ flags : FrameworkPropertyMetadataOptions . Inherits )
172
+ ) ;
173
+
174
+ public static TimeSpan GetShadowAnimationDuration ( DependencyObject element ) => ( TimeSpan ) element . GetValue ( ShadowAnimationDurationProperty ) ;
175
+ public static void SetShadowAnimationDuration ( DependencyObject element , TimeSpan value ) => element . SetValue ( ShadowAnimationDurationProperty , value ) ;
176
+ #endregion
132
177
}
133
178
}
0 commit comments