11using System ;
2- using System . ComponentModel ;
32using System . Windows . Controls ;
43using System . Windows ;
54using System . Windows . Media ;
@@ -17,11 +16,51 @@ 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 FrameworkElement GetAnimationTarget ( Expander expander )
39+ {
40+ if ( expander . GetValue ( CachedToAnimateControlProperty ) is FrameworkElement target )
41+ {
42+ return target ;
43+ }
44+
45+ var animationTargetName = GetToAnimateControlName ( expander ) ;
46+
47+ if ( expander . Template ? . FindName ( animationTargetName , expander ) is not FrameworkElement toAnimateControl )
48+ {
49+ expander . ApplyTemplate ( ) ;
50+ toAnimateControl = expander . Template ? . FindName ( animationTargetName , expander ) as FrameworkElement ??
51+ 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 } ") ;
52+ }
53+
54+ expander . SetValue ( CachedToAnimateControlProperty , toAnimateControl ) ;
55+ return toAnimateControl ;
56+ }
57+
58+ private static readonly DependencyProperty CachedToAnimateControlProperty =
59+ DependencyProperty . RegisterAttached (
60+ "CachedToAnimateControl" ,
61+ typeof ( FrameworkElement ) ,
62+ typeof ( ExpanderAnimationsHelper ) ,
63+ new PropertyMetadata ( null ) ) ;
2564
2665 #endregion
2766
@@ -132,7 +171,7 @@ private static void InitializeExpanderState(Expander expander)
132171 {
133172 // On initial load, set the content to the appropriate state immediately
134173 // without animation to avoid a visual flash
135- var toAnimateControl = GetToAnimateControl ( expander ) ;
174+ var toAnimateControl = GetAnimationTarget ( expander ) ;
136175 if ( toAnimateControl == null )
137176 {
138177 return ;
@@ -174,8 +213,10 @@ private static void RunExpanderAnimation(Expander expander)
174213
175214 private static void AnimateExpand ( Expander expander )
176215 {
177- var toAnimateControl = GetToAnimateControl ( expander ) ;
216+ var toAnimateControl = GetAnimationTarget ( expander ) ;
178217 toAnimateControl . BeginAnimation ( UIElement . VisibilityProperty , null ) ;
218+ toAnimateControl . Visibility = Visibility . Visible ;
219+
179220 UpdateLayout ( toAnimateControl ) ;
180221
181222 if ( toAnimateControl . RenderTransform is not TranslateTransform translateTransform )
@@ -189,7 +230,7 @@ private static void AnimateExpand(Expander expander)
189230
190231 private static void AnimateCollapse ( Expander expander )
191232 {
192- var toAnimateControl = GetToAnimateControl ( expander ) ;
233+ var toAnimateControl = GetAnimationTarget ( expander ) ;
193234 var animationDuration = GetCollapseAnimationDuration ( expander ) ;
194235
195236 var visibilityAnimation = new ObjectAnimationUsingKeyFrames
@@ -269,8 +310,5 @@ private static void UpdateLayout(FrameworkElement contentControl)
269310 contentControl . Measure ( new Size ( contentControl . MaxWidth , contentControl . MaxHeight ) ) ;
270311 contentControl . UpdateLayout ( ) ;
271312 }
272-
273- private static FrameworkElement GetToAnimateControl ( Expander expander ) =>
274- expander . Template ? . FindName ( GetToAnimateControlName ( expander ) , expander ) as FrameworkElement ;
275313 }
276314}
0 commit comments