11using System ;
2- using System . ComponentModel ;
32using System . Windows . Controls ;
43using System . Windows ;
54using System . Windows . Media ;
@@ -17,11 +16,31 @@ public static string GetToAnimateControlName(Expander element) =>
1716 public static void SetToAnimateControlName ( Expander element , string value ) =>
1817 element . SetValue ( ToAnimateControlNameProperty , value ) ;
1918
19+ private const string DefaultAnimationTargetPartName = "ExpanderContent" ;
2020 public static readonly DependencyProperty ToAnimateControlNameProperty = DependencyProperty . RegisterAttached (
2121 "ToAnimateControlName" ,
2222 typeof ( string ) ,
2323 typeof ( ExpanderAnimationsHelper ) ,
24- new PropertyMetadata ( "ExpanderContent" ) ) ;
24+ new PropertyMetadata ( DefaultAnimationTargetPartName , OnToAnimateControlNameChanged ) ) ;
25+
26+ private static void OnToAnimateControlNameChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
27+ {
28+ if ( d is Expander expander )
29+ {
30+ expander . SetValue ( CachedToAnimateControlProperty , null ) ;
31+ }
32+ }
33+
34+ #endregion
35+
36+ #region CachedToAnimateControl
37+
38+ private static readonly DependencyProperty CachedToAnimateControlProperty =
39+ DependencyProperty . RegisterAttached (
40+ "CachedToAnimateControl" ,
41+ typeof ( FrameworkElement ) ,
42+ typeof ( ExpanderAnimationsHelper ) ,
43+ new PropertyMetadata ( null ) ) ;
2544
2645 #endregion
2746
@@ -176,6 +195,8 @@ private static void AnimateExpand(Expander expander)
176195 {
177196 var toAnimateControl = GetToAnimateControl ( expander ) ;
178197 toAnimateControl . BeginAnimation ( UIElement . VisibilityProperty , null ) ;
198+ toAnimateControl . Visibility = Visibility . Visible ;
199+
179200 UpdateLayout ( toAnimateControl ) ;
180201
181202 if ( toAnimateControl . RenderTransform is not TranslateTransform translateTransform )
@@ -270,7 +291,24 @@ private static void UpdateLayout(FrameworkElement contentControl)
270291 contentControl . UpdateLayout ( ) ;
271292 }
272293
273- private static FrameworkElement GetToAnimateControl ( Expander expander ) =>
274- expander . Template ? . FindName ( GetToAnimateControlName ( expander ) , expander ) as FrameworkElement ;
294+ private static FrameworkElement GetToAnimateControl ( Expander expander )
295+ {
296+ if ( expander . GetValue ( CachedToAnimateControlProperty ) is FrameworkElement target )
297+ {
298+ return target ;
299+ }
300+
301+ var animationTargetName = GetToAnimateControlName ( expander ) ;
302+
303+ if ( expander . Template ? . FindName ( animationTargetName , expander ) is not FrameworkElement toAnimateControl )
304+ {
305+ expander . ApplyTemplate ( ) ;
306+ toAnimateControl = expander . Template ? . FindName ( animationTargetName , expander ) as FrameworkElement ??
307+ throw new ArgumentNullException ( "ToAnimateControl" , $ "Couldn't find the Border part to animate either update the ExpanderAnimationsHelper.ToAnimateControlName or rename the animation target part to default: { DefaultAnimationTargetPartName } ") ;
308+ }
309+
310+ expander . SetValue ( CachedToAnimateControlProperty , toAnimateControl ) ;
311+ return toAnimateControl ;
312+ }
275313 }
276314}
0 commit comments