Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose TreeViewItem expansion status to root event. #16984

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Avalonia.Controls/TreeViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Threading;

Expand Down Expand Up @@ -43,6 +44,18 @@ public class TreeViewItem : HeaderedItemsControl, ISelectable
public static readonly DirectProperty<TreeViewItem, int> LevelProperty =
AvaloniaProperty.RegisterDirect<TreeViewItem, int>(
nameof(Level), o => o.Level);

/// <summary>
/// Defines the <see cref="Expanded"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> ExpandedEvent =
RoutedEvent.Register<TreeViewItem, RoutedEventArgs>(nameof(Expanded), RoutingStrategies.Bubble | RoutingStrategies.Tunnel);

/// <summary>
/// Defines the <see cref="Collapsed"/> event.
/// </summary>
public static readonly RoutedEvent<RoutedEventArgs> CollapsedEvent =
RoutedEvent.Register<TreeViewItem, RoutedEventArgs>(nameof(Collapsed), RoutingStrategies.Bubble | RoutingStrategies.Tunnel);

private static readonly FuncTemplate<Panel?> DefaultPanel =
new(() => new StackPanel());
Expand All @@ -65,6 +78,14 @@ static TreeViewItem()
ItemsPanelProperty.OverrideDefaultValue<TreeViewItem>(DefaultPanel);
AutomationProperties.IsOffscreenBehaviorProperty.OverrideDefaultValue<TreeViewItem>(IsOffscreenBehavior.FromClip);
RequestBringIntoViewEvent.AddClassHandler<TreeViewItem>((x, e) => x.OnRequestBringIntoView(e));
IsExpandedProperty.Changed.AddClassHandler<TreeViewItem, bool>((x, e) => x.OnIsExpandedChanged(e));
}

private void OnIsExpandedChanged(AvaloniaPropertyChangedEventArgs<bool> args)
{
var routedEvent = args.NewValue.Value ? ExpandedEvent : CollapsedEvent;
var eventArgs = new RoutedEventArgs() { RoutedEvent = routedEvent, Source = this };
TreeViewOwner?.RaiseEvent(eventArgs);
}

/// <summary>
Expand Down Expand Up @@ -93,6 +114,24 @@ public int Level
get => _level;
private set => SetAndRaise(LevelProperty, ref _level, value);
}

/// <summary>
/// Occurs after the <see cref="TreeViewItem"/> has expanded to show its children.
/// </summary>
public event EventHandler<RoutedEventArgs>? Expanded
{
add => AddHandler(ExpandedEvent, value);
remove => RemoveHandler(ExpandedEvent, value);
}

/// <summary>
/// Occurs after the <see cref="TreeViewItem"/> has collapsed to hide its children.
/// </summary>
public event EventHandler<RoutedEventArgs>? Collapsed
{
add => AddHandler(CollapsedEvent, value);
remove => RemoveHandler(CollapsedEvent, value);
}

internal TreeView? TreeViewOwner => _treeView;

Expand Down
Loading