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