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 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()
{
SelectedItemProperty.Changed.AddClassHandler<NavMenu, object?>((o, e) => o.OnSelectedItemChange(e));
@@ -169,11 +177,20 @@ public class NavMenu: ItemsControl
/// <param name="args"></param>
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;
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;
}

View File

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

View File

@@ -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);
}
}
}