11using System ;
22using System . Windows ;
3+ using System . Windows . Controls ;
4+ using System . Windows . Controls . Primitives ;
35using System . Windows . Media ;
46using System . Windows . Media . Animation ;
57using System . Windows . Media . Effects ;
68
79namespace MaterialDesignThemes . Wpf
810{
11+
912 public enum ShadowDepth
1013 {
1114 Depth0 ,
@@ -37,8 +40,10 @@ public ShadowLocalInfo(double standardOpacity)
3740 public double StandardOpacity { get ; }
3841 }
3942
40- public static class ShadowAssist
43+ public class ShadowAssist
4144 {
45+
46+ #region AttachedProperty : ShadowDepthProperty
4247 public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty . RegisterAttached (
4348 "ShadowDepth" , typeof ( ShadowDepth ) , typeof ( ShadowAssist ) , new FrameworkPropertyMetadata ( default ( ShadowDepth ) , FrameworkPropertyMetadataOptions . AffectsRender ) ) ;
4449
@@ -51,7 +56,9 @@ public static ShadowDepth GetShadowDepth(DependencyObject element)
5156 {
5257 return ( ShadowDepth ) element . GetValue ( ShadowDepthProperty ) ;
5358 }
59+ #endregion
5460
61+ #region AttachedProperty : LocalInfoPropertyKey
5562 private static readonly DependencyPropertyKey LocalInfoPropertyKey = DependencyProperty . RegisterAttachedReadOnly (
5663 "LocalInfo" , typeof ( ShadowLocalInfo ) , typeof ( ShadowAssist ) , new PropertyMetadata ( default ( ShadowLocalInfo ) ) ) ;
5764
@@ -60,24 +67,35 @@ private static void SetLocalInfo(DependencyObject element, ShadowLocalInfo? valu
6067
6168 private static ShadowLocalInfo ? GetLocalInfo ( DependencyObject element )
6269 => ( ShadowLocalInfo ? ) element . GetValue ( LocalInfoPropertyKey . DependencyProperty ) ;
70+ #endregion
6371
72+ #region AttachedProperty : DarkenProperty
6473 public static readonly DependencyProperty DarkenProperty = DependencyProperty . RegisterAttached (
6574 "Darken" , typeof ( bool ) , typeof ( ShadowAssist ) , new FrameworkPropertyMetadata ( default ( bool ) , FrameworkPropertyMetadataOptions . AffectsRender , DarkenPropertyChangedCallback ) ) ;
6675
6776 private static void DarkenPropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
6877 {
78+
6979 var uiElement = dependencyObject as UIElement ;
7080 var dropShadowEffect = uiElement ? . Effect as DropShadowEffect ;
7181
82+
7283 if ( dropShadowEffect == null ) return ;
7384
7485 if ( ( bool ) dependencyPropertyChangedEventArgs . NewValue )
7586 {
7687 SetLocalInfo ( dependencyObject , new ShadowLocalInfo ( dropShadowEffect . Opacity ) ) ;
7788
78- var doubleAnimation = new DoubleAnimation ( 1 , new Duration ( TimeSpan . FromMilliseconds ( 350 ) ) )
89+ TimeSpan time = GetShadowAnimationDuration ( dependencyObject ) ;
90+
91+ var doubleAnimation = new DoubleAnimation ( )
7992 {
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
8199 } ;
82100 dropShadowEffect . BeginAnimation ( DropShadowEffect . OpacityProperty , doubleAnimation ) ;
83101 }
@@ -86,9 +104,16 @@ private static void DarkenPropertyChangedCallback(DependencyObject dependencyObj
86104 var shadowLocalInfo = GetLocalInfo ( dependencyObject ) ;
87105 if ( shadowLocalInfo == null ) return ;
88106
89- var doubleAnimation = new DoubleAnimation ( shadowLocalInfo . StandardOpacity , new Duration ( TimeSpan . FromMilliseconds ( 350 ) ) )
107+ TimeSpan time = GetShadowAnimationDuration ( dependencyObject ) ;
108+
109+ var doubleAnimation = new DoubleAnimation ( )
90110 {
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
92117 } ;
93118 dropShadowEffect . BeginAnimation ( DropShadowEffect . OpacityProperty , doubleAnimation ) ;
94119 }
@@ -103,7 +128,9 @@ public static bool GetDarken(DependencyObject element)
103128 {
104129 return ( bool ) element . GetValue ( DarkenProperty ) ;
105130 }
131+ #endregion
106132
133+ #region AttachedProperty : CacheModeProperty
107134 public static readonly DependencyProperty CacheModeProperty = DependencyProperty . RegisterAttached (
108135 "CacheMode" , typeof ( CacheMode ) , typeof ( ShadowAssist ) , new FrameworkPropertyMetadata ( new BitmapCache { EnableClearType = true , SnapsToDevicePixels = true } , FrameworkPropertyMetadataOptions . Inherits ) ) ;
109136
@@ -116,7 +143,9 @@ public static CacheMode GetCacheMode(DependencyObject element)
116143 {
117144 return ( CacheMode ) element . GetValue ( CacheModeProperty ) ;
118145 }
146+ #endregion
119147
148+ #region AttachedProperty : ShadowEdgesProperty
120149 public static readonly DependencyProperty ShadowEdgesProperty = DependencyProperty . RegisterAttached (
121150 "ShadowEdges" , typeof ( ShadowEdges ) , typeof ( ShadowAssist ) , new PropertyMetadata ( ShadowEdges . All ) ) ;
122151
@@ -129,5 +158,21 @@ public static ShadowEdges GetShadowEdges(DependencyObject element)
129158 {
130159 return ( ShadowEdges ) element . GetValue ( ShadowEdgesProperty ) ;
131160 }
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
132177 }
133178}
0 commit comments