feat: add selection change event.

This commit is contained in:
rabbitism
2024-02-14 21:07:28 +08:00
parent 2918edbe0e
commit cfb728c9e9
3 changed files with 31 additions and 13 deletions

View File

@@ -135,6 +135,14 @@ public class NavMenu: ItemsControl
public static void SetCanToggle(InputElement obj, bool value) => obj.SetValue(CanToggleProperty, value); public static void SetCanToggle(InputElement obj, bool value) => obj.SetValue(CanToggleProperty, value);
public static bool GetCanToggle(InputElement obj) => obj.GetValue(CanToggleProperty); public static bool GetCanToggle(InputElement obj) => obj.GetValue(CanToggleProperty);
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent = RoutedEvent.Register<NavMenu, SelectionChangedEventArgs>(nameof(SelectionChanged), RoutingStrategies.Bubble);
public event EventHandler<SelectionChangedEventArgs>? SelectionChanged
{
add => AddHandler(SelectionChangedEvent, value);
remove => RemoveHandler(SelectionChangedEvent, value);
}
static NavMenu() static NavMenu()
{ {
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e)); SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
@@ -169,11 +177,20 @@ public class NavMenu: ItemsControl
/// <param name="args"></param> /// <param name="args"></param>
private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs<object?> args) private void OnSelectedItemChange(AvaloniaPropertyChangedEventArgs<object?> 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; var newValue = args.NewValue.Value;
if (newValue is null) if (newValue is null)
{ {
ClearAll(); ClearAll();
RaiseEvent(a);
return; return;
} }
var leaves = GetLeafMenus(); var leaves = GetLeafMenus();
@@ -186,8 +203,11 @@ public class NavMenu: ItemsControl
found = true; found = true;
} }
} }
if (found) return; if (!found)
ClearAll(); {
ClearAll();
}
RaiseEvent(a);
} }
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) 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) internal void SelectItem(NavMenuItem item, NavMenuItem parent)
{ {
_updateFromUI = true; _updateFromUI = true;
// if (item.IsSelected) return;
foreach (var child in LogicalChildren) foreach (var child in LogicalChildren)
{ {
if (child == parent) if (child == parent)
@@ -226,7 +245,6 @@ public class NavMenu: ItemsControl
SelectedItem = item; SelectedItem = item;
} }
item.BringIntoView(); item.BringIntoView();
// item.IsSelected = true;
_updateFromUI = false; _updateFromUI = false;
} }

View File

@@ -18,7 +18,7 @@ namespace Ursa.Controls;
/// Navigation Menu Item /// Navigation Menu Item
/// </summary> /// </summary>
[PseudoClasses(PC_Highlighted, PC_HorizontalCollapsed, PC_VerticalCollapsed, PC_FirstLevel, PC_Selector)] [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_Highlighted = ":highlighted";
public const string PC_FirstLevel = ":first-level"; public const string PC_FirstLevel = ":first-level";
@@ -30,7 +30,6 @@ public class NavMenuItem: HeaderedSelectingItemsControl
private Panel? _popupPanel; private Panel? _popupPanel;
private Popup? _popup; private Popup? _popup;
private Panel? _overflowPanel; private Panel? _overflowPanel;
private Border? _border;
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>( public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
nameof(Icon)); nameof(Icon));
@@ -67,7 +66,7 @@ public class NavMenuItem: HeaderedSelectingItemsControl
set => SetValue(CommandParameterProperty, value); set => SetValue(CommandParameterProperty, value);
} }
public new static readonly StyledProperty<bool> IsSelectedProperty = public static readonly StyledProperty<bool> IsSelectedProperty =
SelectingItemsControl.IsSelectedProperty.AddOwner<NavMenuItem>(); SelectingItemsControl.IsSelectedProperty.AddOwner<NavMenuItem>();
public bool IsSelected public bool IsSelected
@@ -75,6 +74,8 @@ public class NavMenuItem: HeaderedSelectingItemsControl
get => GetValue(IsSelectedProperty); get => GetValue(IsSelectedProperty);
set => SetValue(IsSelectedProperty, value); set => SetValue(IsSelectedProperty, value);
} }
public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent = RoutedEvent.Register<SelectingItemsControl, RoutedEventArgs>("IsSelectedChanged", RoutingStrategies.Bubble);
private bool _isHighlighted; private bool _isHighlighted;
@@ -192,7 +193,6 @@ public class NavMenuItem: HeaderedSelectingItemsControl
SetCurrentValue(LevelProperty,CalculateDistanceFromLogicalParent<NavMenu>(this)); SetCurrentValue(LevelProperty,CalculateDistanceFromLogicalParent<NavMenu>(this));
_popup = e.NameScope.Find<Popup>("PART_Popup"); _popup = e.NameScope.Find<Popup>("PART_Popup");
_overflowPanel = e.NameScope.Find<Panel>("PART_OverflowPanel"); _overflowPanel = e.NameScope.Find<Panel>("PART_OverflowPanel");
_border = e.NameScope.Find<Border>("PART_Border");
if (_rootMenu is not null) if (_rootMenu is not null)
{ {
if (_rootMenu.IconBinding is not null) if (_rootMenu.IconBinding is not null)

View File

@@ -10,8 +10,8 @@ public class OverflowStackPanel: StackPanel
var children = this.Children.ToList(); var children = this.Children.ToList();
foreach (var child in children) foreach (var child in children)
{ {
this.Children.Remove(child); Children.Remove(child);
this.OverflowPanel?.Children.Add(child); OverflowPanel?.Children.Add(child);
} }
} }
@@ -22,8 +22,8 @@ public class OverflowStackPanel: StackPanel
{ {
foreach (var child in children) foreach (var child in children)
{ {
this.OverflowPanel?.Children.Remove(child); OverflowPanel?.Children.Remove(child);
this.Children.Add(child); Children.Add(child);
} }
} }
} }