feat: stop changing selection after checking canselect. revert focus change in this case.

This commit is contained in:
Dong Bin
2025-06-12 19:30:14 +08:00
parent 0e83446cb6
commit fb9f03d63b
3 changed files with 19 additions and 9 deletions

View File

@@ -191,10 +191,10 @@ public class NavMenu : ItemsControl, ICustomKeyboardNavigation
} }
public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangingEvent = public static readonly RoutedEvent<SelectionChangingEventArgs> SelectionChangingEvent =
RoutedEvent.Register<NavMenu, SelectionChangedEventArgs>(nameof(SelectionChanging), RoutingStrategies.Bubble); RoutedEvent.Register<NavMenu, SelectionChangingEventArgs>(nameof(SelectionChanging), RoutingStrategies.Bubble);
public event EventHandler SelectionChanging public event EventHandler<SelectionChangingEventArgs> SelectionChanging
{ {
add => AddHandler(SelectionChangingEvent, value); add => AddHandler(SelectionChangingEvent, value);
remove => RemoveHandler(SelectionChangingEvent, value); remove => RemoveHandler(SelectionChangingEvent, value);
@@ -512,6 +512,12 @@ public class NavMenu : ItemsControl, ICustomKeyboardNavigation
Source = this, Source = this,
}; };
RaiseEvent(args); RaiseEvent(args);
return args.CanSelect; var result = args.CanSelect;
if (result == false)
{
var container = GetContainerForItem(SelectedItem);
container?.Focus(NavigationMethod.Directional);
}
return result;
} }
} }

View File

@@ -381,6 +381,10 @@ public class NavMenuItem : HeaderedItemsControl
internal void SelectItem(NavMenuItem item) internal void SelectItem(NavMenuItem item)
{ {
if (item == this && RootMenu?.CanChangeSelection(item) != true)
{
return;
}
SetCurrentValue(IsSelectedProperty, item == this); SetCurrentValue(IsSelectedProperty, item == this);
SetCurrentValue(IsHighlightedProperty, true); SetCurrentValue(IsHighlightedProperty, true);

View File

@@ -6,19 +6,19 @@ namespace Ursa.Controls;
public class SelectionChangingEventArgs: RoutedEventArgs public class SelectionChangingEventArgs: RoutedEventArgs
{ {
/// <summary>Gets the items that were added to the selection.</summary> /// <summary>Gets the items that were added to the selection.</summary>
public IList AddedItems { get; } public IList NewItems { get; }
/// <summary>Gets the items that were removed from the selection.</summary> /// <summary>Gets the items that were removed from the selection.</summary>
public IList RemovedItems { get; } public IList OldItems { get; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether the selection can be changed. If set to <c>false</c>, the selection will not change. /// Gets or sets a value indicating whether the selection can be changed. If set to <c>false</c>, the selection will not change.
/// </summary> /// </summary>
public bool CanSelect { get; set; } = true; public bool CanSelect { get; set; } = true;
public SelectionChangingEventArgs(RoutedEvent routedEvent, IList removedItems, IList addedItems): base(routedEvent) public SelectionChangingEventArgs(RoutedEvent routedEvent, IList oldItems, IList newItems): base(routedEvent)
{ {
RemovedItems = removedItems; OldItems = oldItems;
AddedItems = addedItems; NewItems = newItems;
} }
} }