Skip to content

Commit 753b852

Browse files
committed
Various improvements to ExpanderAnimationBehavior including Expanded and Collapsed event handlers
1 parent e075ed6 commit 753b852

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/Expander/ExpanderPage.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<mct:Expander.Content>
4949
<Label Text="Item 1" />
5050
</mct:Expander.Content>
51+
<mct:Expander.Behaviors>
52+
<mct:ExpanderAnimationBehavior CollapsingLength="250" ExpandingLength="250"/>
53+
</mct:Expander.Behaviors>
5154
</mct:Expander>
5255
</mct:Expander.Content>
5356
<mct:Expander.Behaviors>

src/CommunityToolkit.Maui/Behaviors/ExpanderAnimationBehavior.shared.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ public partial class ExpanderAnimationBehavior : BaseBehavior<Expander>
1212
/// Backing BindableProperty for the <see cref="CollapsingLength"/> property.
1313
/// </summary>
1414
public static readonly BindableProperty CollapsingLengthProperty =
15-
BindableProperty.CreateAttached(nameof(CollapsingLength), typeof(uint), typeof(ExpanderAnimationBehavior), 250u);
15+
BindableProperty.Create(nameof(CollapsingLength), typeof(uint), typeof(ExpanderAnimationBehavior), 250u);
1616

1717
/// <summary>
1818
/// Backing BindableProperty for the <see cref="CollapsingEasing"/> property.
1919
/// </summary>
2020
public static readonly BindableProperty CollapsingEasingProperty =
21-
BindableProperty.CreateAttached(nameof(CollapsingEasing), typeof(Easing), typeof(ExpanderAnimationBehavior), Easing.Linear);
21+
BindableProperty.Create(nameof(CollapsingEasing), typeof(Easing), typeof(ExpanderAnimationBehavior), Easing.Linear);
2222

2323
/// <summary>
2424
/// Backing BindableProperty for the <see cref="ExpandingLength"/> property.
2525
/// </summary>
2626
public static readonly BindableProperty ExpandingLengthProperty =
27-
BindableProperty.CreateAttached(nameof(ExpandingLength), typeof(uint), typeof(ExpanderAnimationBehavior), 250u);
27+
BindableProperty.Create(nameof(ExpandingLength), typeof(uint), typeof(ExpanderAnimationBehavior), 250u);
2828

2929
/// <summary>
3030
/// Backing BindableProperty for the <see cref="ExpandingEasing"/> property.
3131
/// </summary>
3232
public static readonly BindableProperty ExpandingEasingProperty =
33-
BindableProperty.CreateAttached(nameof(ExpandingEasing), typeof(Easing), typeof(ExpanderAnimationBehavior), Easing.Linear);
33+
BindableProperty.Create(nameof(ExpandingEasing), typeof(Easing), typeof(ExpanderAnimationBehavior), Easing.Linear);
3434

3535
/// <summary>
3636
/// Length in milliseconds of the collapse animation when the <see cref="Expander"/> is collapsing.
@@ -50,9 +50,7 @@ public Easing CollapsingEasing
5050
set => SetValue(CollapsingEasingProperty, value);
5151
}
5252

53-
/// <summary>
54-
/// Length in milliseconds of the expand animation when the <see cref="Expander"/> is expanding.
55-
/// </summary>
53+
/// <summary>Length in milliseconds of the expand animation when the <see cref="Expander"/> is expanding.</summary>
5654
public uint ExpandingLength
5755
{
5856
get => (uint)GetValue(ExpandingLengthProperty);
@@ -68,6 +66,16 @@ public Easing ExpandingEasing
6866
set => SetValue(ExpandingEasingProperty, value);
6967
}
7068

69+
/// <summary>
70+
/// Occurs when the animation for the <see cref="Expander"/> finishes collapsing.
71+
/// </summary>
72+
public event EventHandler Collapsed;
73+
74+
/// <summary>
75+
/// Occurs when the animation for the <see cref="Expander"/> finishes expanding.
76+
/// </summary>
77+
public event EventHandler Expanded;
78+
7179
/// <summary>
7280
///
7381
/// </summary>
@@ -82,17 +90,21 @@ protected override void OnViewPropertyChanged(Expander sender, PropertyChangedEv
8290
case nameof(Expander.IsExpanded):
8391
if (sender.IsExpanded)
8492
{
85-
AnimateContentHeight(sender, 1.0, sender.BodyContentView.Height, ExpandingLength, ExpandingEasing);
93+
sender.Dispatcher.Dispatch(async () =>
94+
{
95+
await AnimateContentHeight(sender, 1.0, sender.BodyContentView.Height, ExpandingLength, ExpandingEasing);
96+
Expanded?.Invoke(sender, EventArgs.Empty);
97+
});
8698
}
8799
else
88100
{
89-
AnimateContentHeight(sender, sender.BodyContentView.Height, 1.0, ExpandingLength, ExpandingEasing);
101+
sender.Dispatcher.Dispatch(async () =>
102+
{
103+
await AnimateContentHeight(sender, sender.BodyContentView.Height, 1.0, CollapsingLength, CollapsingEasing);
104+
Collapsed?.Invoke(sender, EventArgs.Empty);
105+
});
90106
}
91107
break;
92-
case nameof(Expander.Header):
93-
case nameof(Expander.Content):
94-
sender.InvalidateMeasure();
95-
break;
96108
}
97109
}
98110

0 commit comments

Comments
 (0)