diff --git a/src/Avalonia.Controls/TreeViewItem.cs b/src/Avalonia.Controls/TreeViewItem.cs index 9517a477e9..871d952678 100644 --- a/src/Avalonia.Controls/TreeViewItem.cs +++ b/src/Avalonia.Controls/TreeViewItem.cs @@ -11,6 +11,7 @@ using Avalonia.Controls.Selection; using Avalonia.Controls.Templates; using Avalonia.Data; using Avalonia.Input; +using Avalonia.Interactivity; using Avalonia.LogicalTree; using Avalonia.Threading; @@ -43,6 +44,18 @@ namespace Avalonia.Controls public static readonly DirectProperty LevelProperty = AvaloniaProperty.RegisterDirect( nameof(Level), o => o.Level); + + /// + /// Defines the event. + /// + public static readonly RoutedEvent ExpandedEvent = + RoutedEvent.Register(nameof(Expanded), RoutingStrategies.Bubble | RoutingStrategies.Tunnel); + + /// + /// Defines the event. + /// + public static readonly RoutedEvent CollapsedEvent = + RoutedEvent.Register(nameof(Collapsed), RoutingStrategies.Bubble | RoutingStrategies.Tunnel); private static readonly FuncTemplate DefaultPanel = new(() => new StackPanel()); @@ -65,6 +78,14 @@ namespace Avalonia.Controls ItemsPanelProperty.OverrideDefaultValue(DefaultPanel); AutomationProperties.IsOffscreenBehaviorProperty.OverrideDefaultValue(IsOffscreenBehavior.FromClip); RequestBringIntoViewEvent.AddClassHandler((x, e) => x.OnRequestBringIntoView(e)); + IsExpandedProperty.Changed.AddClassHandler((x, e) => x.OnIsExpandedChanged(e)); + } + + private void OnIsExpandedChanged(AvaloniaPropertyChangedEventArgs args) + { + var routedEvent = args.NewValue.Value ? ExpandedEvent : CollapsedEvent; + var eventArgs = new RoutedEventArgs() { RoutedEvent = routedEvent, Source = this }; + TreeViewOwner?.RaiseEvent(eventArgs); } /// @@ -93,6 +114,24 @@ namespace Avalonia.Controls get => _level; private set => SetAndRaise(LevelProperty, ref _level, value); } + + /// + /// Occurs after the has expanded to show its children. + /// + public event EventHandler? Expanded + { + add => AddHandler(ExpandedEvent, value); + remove => RemoveHandler(ExpandedEvent, value); + } + + /// + /// Occurs after the has collapsed to hide its children. + /// + public event EventHandler? Collapsed + { + add => AddHandler(CollapsedEvent, value); + remove => RemoveHandler(CollapsedEvent, value); + } internal TreeView? TreeViewOwner => _treeView;