@@ -14,8 +14,8 @@ public sealed partial class ExpandContent : UserControl
1414{
1515 public UIElementCollection Children => CollapsiblePanel . Children ;
1616
17- private bool ? _pendingIsOpen ;
18-
17+ private readonly string _expandedState = "ExpandedState" ;
18+ private readonly string _collapsedState = "CollapsedState" ;
1919
2020 public ExpandContent ( )
2121 {
@@ -26,8 +26,8 @@ public ExpandContent()
2626 // we need to set the initial state based on IsOpen.
2727 VisualStateManager . GoToState (
2828 this ,
29- IsOpen ? "ExpandedState" : "CollapsedState" ,
30- useTransitions : false ) ; // ← NO animation yet
29+ IsOpen ? _expandedState : _collapsedState ,
30+ useTransitions : false ) ; // NO animation yet
3131
3232 // If IsOpen was already true we must also show the panel
3333 if ( IsOpen )
@@ -41,59 +41,21 @@ public ExpandContent()
4141
4242 partial void OnIsOpenChanged ( bool oldValue , bool newValue )
4343 {
44- if ( ! IsLoaded )
44+ var newState = newValue ? _expandedState : _collapsedState ;
45+ if ( newValue )
4546 {
46- _pendingIsOpen = newValue ;
47- return ;
48- }
49- _ = AnimateAsync ( newValue ) ;
50- }
51-
52- private async Task AnimateAsync ( bool open )
53- {
54- if ( open )
55- {
56- if ( _currentlyOpen is not null && _currentlyOpen != this )
57- await _currentlyOpen . StartCollapseAsync ( ) ;
58-
59- _currentlyOpen = this ;
6047 CollapsiblePanel . Visibility = Visibility . Visible ;
61-
62- VisualStateManager . GoToState ( this , "ExpandedState" , true ) ;
63- await ExpandAsync ( ) ;
48+ // We use BeginTime to ensure other panels are collapsed first.
49+ // If the user clicks the expand button quickly, we want to avoid
50+ // the panel expanding to its full height before the collapse animation completes.
51+ CollapseSb . SkipToFill ( ) ;
6452 }
65- else
66- {
67- if ( _currentlyOpen == this ) _currentlyOpen = null ;
68- await StartCollapseAsync ( ) ;
69- }
70- }
71-
72- private static ExpandContent ? _currentlyOpen ;
73- private TaskCompletionSource ? _collapseTcs ;
74-
75- private async Task ExpandAsync ( )
76- {
77- CollapsiblePanel . Visibility = Visibility . Visible ;
78- VisualStateManager . GoToState ( this , "ExpandedState" , true ) ;
79-
80- var tcs = new TaskCompletionSource ( ) ;
81- void done ( object ? s , object e ) { ExpandSb . Completed -= done ; tcs . SetResult ( ) ; }
82- ExpandSb . Completed += done ;
83- await tcs . Task ;
84- }
8553
86- private Task StartCollapseAsync ( )
87- {
88- _collapseTcs = new TaskCompletionSource ( ) ;
89- VisualStateManager . GoToState ( this , "CollapsedState" , true ) ;
90- return _collapseTcs . Task ;
54+ VisualStateManager . GoToState ( this , newState , true ) ;
9155 }
9256
9357 private void CollapseStoryboard_Completed ( object sender , object e )
9458 {
9559 CollapsiblePanel . Visibility = Visibility . Collapsed ;
96- _collapseTcs ? . TrySetResult ( ) ;
97- _collapseTcs = null ;
9860 }
9961}
0 commit comments