diff --git a/src/Ursa/Controls/NavMenu/NavMenu.cs b/src/Ursa/Controls/NavMenu/NavMenu.cs index ec00428..dabec9a 100644 --- a/src/Ursa/Controls/NavMenu/NavMenu.cs +++ b/src/Ursa/Controls/NavMenu/NavMenu.cs @@ -189,6 +189,16 @@ public class NavMenu : ItemsControl, ICustomKeyboardNavigation add => AddHandler(SelectionChangedEvent, value); remove => RemoveHandler(SelectionChangedEvent, value); } + + + public static readonly RoutedEvent SelectionChangingEvent = + RoutedEvent.Register(nameof(SelectionChanging), RoutingStrategies.Bubble); + + public event EventHandler SelectionChanging + { + add => AddHandler(SelectionChangingEvent, value); + remove => RemoveHandler(SelectionChangingEvent, value); + } protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey) { @@ -486,4 +496,22 @@ public class NavMenu : ItemsControl, ICustomKeyboardNavigation return null; } + + internal bool CanChangeSelection(NavMenuItem item) + { + object? newSelection = null; + if (item.DataContext is not null && item.DataContext != DataContext) + newSelection = item.DataContext; + else + newSelection = item; + var args = new SelectionChangingEventArgs( + SelectionChangingEvent, + new[] { SelectedItem }, + new[] { newSelection }) + { + Source = this, + }; + RaiseEvent(args); + return args.CanSelect; + } } \ No newline at end of file diff --git a/src/Ursa/Controls/NavMenu/SelectionChangingEventArgs.cs b/src/Ursa/Controls/NavMenu/SelectionChangingEventArgs.cs new file mode 100644 index 0000000..3b5c95b --- /dev/null +++ b/src/Ursa/Controls/NavMenu/SelectionChangingEventArgs.cs @@ -0,0 +1,24 @@ +using System.Collections; +using Avalonia.Interactivity; + +namespace Ursa.Controls; + +public class SelectionChangingEventArgs: RoutedEventArgs +{ + /// Gets the items that were added to the selection. + public IList AddedItems { get; } + + /// Gets the items that were removed from the selection. + public IList RemovedItems { get; } + + /// + /// Gets or sets a value indicating whether the selection can be changed. If set to false, the selection will not change. + /// + public bool CanSelect { get; set; } = true; + + public SelectionChangingEventArgs(RoutedEvent routedEvent, IList removedItems, IList addedItems): base(routedEvent) + { + RemovedItems = removedItems; + AddedItems = addedItems; + } +} \ No newline at end of file