22using System . Collections . Generic ;
33using System . Linq ;
44using System . Windows . Input ;
5- using Avalonia ;
65using Avalonia . Input ;
76using Avalonia . Interactivity ;
87
98namespace AvaloniaEdit
109{
1110 public class RoutedCommand : ICommand
1211 {
12+ private static IInputElement _inputElement ;
13+
1314 public string Name { get ; }
1415 public KeyGesture Gesture { get ; }
1516
@@ -23,6 +24,7 @@ static RoutedCommand()
2324 {
2425 CanExecuteEvent . AddClassHandler < Interactive > ( CanExecuteEventHandler ) ;
2526 ExecutedEvent . AddClassHandler < Interactive > ( ExecutedEventHandler ) ;
27+ InputElement . GotFocusEvent . AddClassHandler < Interactive > ( GotFocusEventHandler ) ;
2628 }
2729
2830 private static void CanExecuteEventHandler ( Interactive control , CanExecuteRoutedEventArgs args )
@@ -46,6 +48,11 @@ private static void ExecutedEventHandler(Interactive control, ExecutedRoutedEven
4648 }
4749 }
4850
51+ private static void GotFocusEventHandler ( Interactive control , GotFocusEventArgs args )
52+ {
53+ _inputElement = control as IInputElement ;
54+ }
55+
4956 public static RoutedEvent < CanExecuteRoutedEventArgs > CanExecuteEvent { get ; } = RoutedEvent . Register < CanExecuteRoutedEventArgs > ( nameof ( CanExecuteEvent ) , RoutingStrategies . Bubble , typeof ( RoutedCommand ) ) ;
5057
5158 public bool CanExecute ( object parameter , IInputElement target )
@@ -60,7 +67,7 @@ public bool CanExecute(object parameter, IInputElement target)
6067
6168 bool ICommand . CanExecute ( object parameter )
6269 {
63- return CanExecute ( parameter , Application . Current . FocusManager . Current ) ;
70+ return CanExecute ( parameter , _inputElement ) ;
6471 }
6572
6673 public static RoutedEvent < ExecutedRoutedEventArgs > ExecutedEvent { get ; } = RoutedEvent . Register < ExecutedRoutedEventArgs > ( nameof ( ExecutedEvent ) , RoutingStrategies . Bubble , typeof ( RoutedCommand ) ) ;
@@ -75,7 +82,7 @@ public void Execute(object parameter, IInputElement target)
7582
7683 void ICommand . Execute ( object parameter )
7784 {
78- Execute ( parameter , Application . Current . FocusManager . Current ) ;
85+ Execute ( parameter , _inputElement ) ;
7986 }
8087
8188 // TODO
0 commit comments