diff --git a/src/Avalonia.Controls/Expander.cs b/src/Avalonia.Controls/Expander.cs
index 3ba99d8a67..5c17f32c68 100644
--- a/src/Avalonia.Controls/Expander.cs
+++ b/src/Avalonia.Controls/Expander.cs
@@ -1,7 +1,10 @@
+using System;
using System.Threading;
using Avalonia.Animation;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
+using Avalonia.Interactivity;
+using Avalonia.Threading;
namespace Avalonia.Controls
{
@@ -37,12 +40,24 @@ namespace Avalonia.Controls
[PseudoClasses(":expanded", ":up", ":down", ":left", ":right")]
public class Expander : HeaderedContentControl
{
+ ///
+ /// Defines the property.
+ ///
public static readonly StyledProperty ContentTransitionProperty =
- AvaloniaProperty.Register(nameof(ContentTransition));
+ AvaloniaProperty.Register(
+ nameof(ContentTransition));
+ ///
+ /// Defines the property.
+ ///
public static readonly StyledProperty ExpandDirectionProperty =
- AvaloniaProperty.Register(nameof(ExpandDirection), ExpandDirection.Down);
+ AvaloniaProperty.Register(
+ nameof(ExpandDirection),
+ ExpandDirection.Down);
+ ///
+ /// Defines the property.
+ ///
public static readonly DirectProperty IsExpandedProperty =
AvaloniaProperty.RegisterDirect(
nameof(IsExpanded),
@@ -50,47 +65,155 @@ namespace Avalonia.Controls
(o, v) => o.IsExpanded = v,
defaultBindingMode: Data.BindingMode.TwoWay);
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent CollapsedEvent =
+ RoutedEvent.Register(
+ nameof(Collapsed),
+ RoutingStrategies.Bubble);
+
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent CollapsingEvent =
+ RoutedEvent.Register(
+ nameof(Collapsing),
+ RoutingStrategies.Bubble);
+
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent ExpandedEvent =
+ RoutedEvent.Register(
+ nameof(Expanded),
+ RoutingStrategies.Bubble);
+
+ ///
+ /// Defines the event.
+ ///
+ public static readonly RoutedEvent ExpandingEvent =
+ RoutedEvent.Register(
+ nameof(Expanding),
+ RoutingStrategies.Bubble);
+
private bool _isExpanded;
private CancellationTokenSource? _lastTransitionCts;
- static Expander()
- {
- IsExpandedProperty.Changed.AddClassHandler((x, e) => x.OnIsExpandedChanged(e));
- }
-
+ ///
+ /// Initializes a new instance of the class.
+ ///
public Expander()
{
- UpdatePseudoClasses(ExpandDirection);
+ UpdatePseudoClasses();
}
+ ///
+ /// Gets or sets the transition used when expanding or collapsing the content.
+ ///
public IPageTransition? ContentTransition
{
get => GetValue(ContentTransitionProperty);
set => SetValue(ContentTransitionProperty, value);
}
+ ///
+ /// Gets or sets the direction in which the opens.
+ ///
public ExpandDirection ExpandDirection
{
get => GetValue(ExpandDirectionProperty);
set => SetValue(ExpandDirectionProperty, value);
}
+ ///
+ /// Gets or sets a value indicating whether the
+ /// content area is open and visible.
+ ///
public bool IsExpanded
{
- get { return _isExpanded; }
- set
- {
- SetAndRaise(IsExpandedProperty, ref _isExpanded, value);
- PseudoClasses.Set(":expanded", value);
- }
+ get => _isExpanded;
+ set => SetAndRaise(IsExpandedProperty, ref _isExpanded, value);
+ }
+
+ ///
+ /// Occurs after the content area has closed and only the header is visible.
+ ///
+ public event EventHandler? Collapsed
+ {
+ add => AddHandler(CollapsedEvent, value);
+ remove => RemoveHandler(CollapsedEvent, value);
+ }
+
+ ///
+ /// Occurs as the content area is closing.
+ ///
+ public event EventHandler? Collapsing
+ {
+ add => AddHandler(CollapsingEvent, value);
+ remove => RemoveHandler(CollapsingEvent, value);
+ }
+
+ ///
+ /// Occurs after the has opened to display both its header and content.
+ ///
+ public event EventHandler? Expanded
+ {
+ add => AddHandler(ExpandedEvent, value);
+ remove => RemoveHandler(ExpandedEvent, value);
+ }
+
+ ///
+ /// Occurs as the content area is opening.
+ ///
+ public event EventHandler? Expanding
+ {
+ add => AddHandler(ExpandingEvent, value);
+ remove => RemoveHandler(ExpandingEvent, value);
+ }
+
+ ///
+ /// Invoked just before the event.
+ ///
+ protected virtual void OnCollapsed(RoutedEventArgs eventArgs)
+ {
+ RaiseEvent(eventArgs);
+ }
+
+ ///
+ /// Invoked just before the event.
+ ///
+ protected virtual void OnCollapsing(RoutedEventArgs eventArgs)
+ {
+ RaiseEvent(eventArgs);
+ }
+
+ ///
+ /// Invoked just before the event.
+ ///
+ protected virtual void OnExpanded(RoutedEventArgs eventArgs)
+ {
+ RaiseEvent(eventArgs);
}
- protected virtual async void OnIsExpandedChanged(AvaloniaPropertyChangedEventArgs e)
+ ///
+ /// Invoked just before the event.
+ ///
+ protected virtual void OnExpanding(RoutedEventArgs eventArgs)
+ {
+ RaiseEvent(eventArgs);
+ }
+
+ ///
+ /// Starts the content transition (if set) and invokes the
+ /// and events when completed.
+ ///
+ private async void StartContentTransition()
{
if (Content != null && ContentTransition != null && Presenter is Visual visualContent)
{
bool forward = ExpandDirection == ExpandDirection.Left ||
- ExpandDirection == ExpandDirection.Up;
+ ExpandDirection == ExpandDirection.Up;
_lastTransitionCts?.Cancel();
_lastTransitionCts = new CancellationTokenSource();
@@ -104,24 +227,62 @@ namespace Avalonia.Controls
await ContentTransition.Start(visualContent, null, forward, _lastTransitionCts.Token);
}
}
+
+ // Expanded/Collapsed events are invoked asynchronously to ensure other events,
+ // such as Click, have time to complete first.
+ Dispatcher.UIThread.Post(() =>
+ {
+ if (IsExpanded)
+ {
+ OnExpanded(new RoutedEventArgs(ExpandedEvent, this));
+ }
+ else
+ {
+ OnCollapsed(new RoutedEventArgs(CollapsedEvent, this));
+ }
+ });
}
+ ///
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == ExpandDirectionProperty)
{
- UpdatePseudoClasses(change.GetNewValue());
+ UpdatePseudoClasses();
+ }
+ else if (change.Property == IsExpandedProperty)
+ {
+ if (IsExpanded)
+ {
+ OnExpanding(new RoutedEventArgs(ExpandingEvent, this));
+ }
+ else
+ {
+ OnCollapsing(new RoutedEventArgs(CollapsingEvent, this));
+ }
+
+ // Expanded/Collapsed will be raised once transitions are complete
+ StartContentTransition();
+
+ UpdatePseudoClasses();
}
}
- private void UpdatePseudoClasses(ExpandDirection d)
+ ///
+ /// Updates the visual state of the control by applying latest PseudoClasses.
+ ///
+ private void UpdatePseudoClasses()
{
- PseudoClasses.Set(":up", d == ExpandDirection.Up);
- PseudoClasses.Set(":down", d == ExpandDirection.Down);
- PseudoClasses.Set(":left", d == ExpandDirection.Left);
- PseudoClasses.Set(":right", d == ExpandDirection.Right);
+ var expandDirection = ExpandDirection;
+
+ PseudoClasses.Set(":up", expandDirection == ExpandDirection.Up);
+ PseudoClasses.Set(":down", expandDirection == ExpandDirection.Down);
+ PseudoClasses.Set(":left", expandDirection == ExpandDirection.Left);
+ PseudoClasses.Set(":right", expandDirection == ExpandDirection.Right);
+
+ PseudoClasses.Set(":expanded", IsExpanded);
}
}
}