diff --git a/src/Ursa/Controls/NavMenu/NavMenu.cs b/src/Ursa/Controls/NavMenu/NavMenu.cs index 0237c8c..44d73f8 100644 --- a/src/Ursa/Controls/NavMenu/NavMenu.cs +++ b/src/Ursa/Controls/NavMenu/NavMenu.cs @@ -135,6 +135,14 @@ public class NavMenu: ItemsControl public static void SetCanToggle(InputElement obj, bool value) => obj.SetValue(CanToggleProperty, value); public static bool GetCanToggle(InputElement obj) => obj.GetValue(CanToggleProperty); + public static readonly RoutedEvent SelectionChangedEvent = RoutedEvent.Register(nameof(SelectionChanged), RoutingStrategies.Bubble); + + public event EventHandler? SelectionChanged + { + add => AddHandler(SelectionChangedEvent, value); + remove => RemoveHandler(SelectionChangedEvent, value); + } + static NavMenu() { SelectedItemProperty.Changed.AddClassHandler((o, e) => o.OnSelectedItemChange(e)); @@ -169,11 +177,20 @@ public class NavMenu: ItemsControl /// private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs args) { - if (_updateFromUI) return; + SelectionChangedEventArgs a = new SelectionChangedEventArgs( + SelectionChangedEvent, + new [] { args.OldValue.Value }, + new [] { args.NewValue.Value }); + if (_updateFromUI) + { + RaiseEvent(a); + return; + } var newValue = args.NewValue.Value; if (newValue is null) { ClearAll(); + RaiseEvent(a); return; } var leaves = GetLeafMenus(); @@ -186,8 +203,11 @@ public class NavMenu: ItemsControl found = true; } } - if (found) return; - ClearAll(); + if (!found) + { + ClearAll(); + } + RaiseEvent(a); } protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) @@ -205,7 +225,6 @@ public class NavMenu: ItemsControl internal void SelectItem(NavMenuItem item, NavMenuItem parent) { _updateFromUI = true; - // if (item.IsSelected) return; foreach (var child in LogicalChildren) { if (child == parent) @@ -226,7 +245,6 @@ public class NavMenu: ItemsControl SelectedItem = item; } item.BringIntoView(); - // item.IsSelected = true; _updateFromUI = false; } diff --git a/src/Ursa/Controls/NavMenu/NavMenuItem.cs b/src/Ursa/Controls/NavMenu/NavMenuItem.cs index 10568d5..e1c31d3 100644 --- a/src/Ursa/Controls/NavMenu/NavMenuItem.cs +++ b/src/Ursa/Controls/NavMenu/NavMenuItem.cs @@ -18,7 +18,7 @@ namespace Ursa.Controls; /// Navigation Menu Item /// [PseudoClasses(PC_Highlighted, PC_HorizontalCollapsed, PC_VerticalCollapsed, PC_FirstLevel, PC_Selector)] -public class NavMenuItem: HeaderedSelectingItemsControl +public class NavMenuItem: HeaderedItemsControl { public const string PC_Highlighted = ":highlighted"; public const string PC_FirstLevel = ":first-level"; @@ -30,7 +30,6 @@ public class NavMenuItem: HeaderedSelectingItemsControl private Panel? _popupPanel; private Popup? _popup; private Panel? _overflowPanel; - private Border? _border; public static readonly StyledProperty IconProperty = AvaloniaProperty.Register( nameof(Icon)); @@ -67,7 +66,7 @@ public class NavMenuItem: HeaderedSelectingItemsControl set => SetValue(CommandParameterProperty, value); } - public new static readonly StyledProperty IsSelectedProperty = + public static readonly StyledProperty IsSelectedProperty = SelectingItemsControl.IsSelectedProperty.AddOwner(); public bool IsSelected @@ -75,6 +74,8 @@ public class NavMenuItem: HeaderedSelectingItemsControl get => GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); } + + public static readonly RoutedEvent IsSelectedChangedEvent = RoutedEvent.Register("IsSelectedChanged", RoutingStrategies.Bubble); private bool _isHighlighted; @@ -192,7 +193,6 @@ public class NavMenuItem: HeaderedSelectingItemsControl SetCurrentValue(LevelProperty,CalculateDistanceFromLogicalParent(this)); _popup = e.NameScope.Find("PART_Popup"); _overflowPanel = e.NameScope.Find("PART_OverflowPanel"); - _border = e.NameScope.Find("PART_Border"); if (_rootMenu is not null) { if (_rootMenu.IconBinding is not null) diff --git a/src/Ursa/Controls/NavMenu/OverflowStackPanel.cs b/src/Ursa/Controls/NavMenu/OverflowStackPanel.cs index b55877a..bfa7ad1 100644 --- a/src/Ursa/Controls/NavMenu/OverflowStackPanel.cs +++ b/src/Ursa/Controls/NavMenu/OverflowStackPanel.cs @@ -10,8 +10,8 @@ public class OverflowStackPanel: StackPanel var children = this.Children.ToList(); foreach (var child in children) { - this.Children.Remove(child); - this.OverflowPanel?.Children.Add(child); + Children.Remove(child); + OverflowPanel?.Children.Add(child); } } @@ -22,8 +22,8 @@ public class OverflowStackPanel: StackPanel { foreach (var child in children) { - this.OverflowPanel?.Children.Remove(child); - this.Children.Add(child); + OverflowPanel?.Children.Remove(child); + Children.Add(child); } } }