2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Windows . Input ;
5
- using Avalonia ;
6
5
using Avalonia . Input ;
7
6
using Avalonia . Interactivity ;
8
7
9
8
namespace AvaloniaEdit
10
9
{
11
10
public class RoutedCommand : ICommand
12
11
{
12
+ private static IInputElement _inputElement ;
13
+
13
14
public string Name { get ; }
14
15
public KeyGesture Gesture { get ; }
15
16
@@ -23,6 +24,7 @@ static RoutedCommand()
23
24
{
24
25
CanExecuteEvent . AddClassHandler < Interactive > ( CanExecuteEventHandler ) ;
25
26
ExecutedEvent . AddClassHandler < Interactive > ( ExecutedEventHandler ) ;
27
+ InputElement . GotFocusEvent . AddClassHandler < Interactive > ( GotFocusEventHandler ) ;
26
28
}
27
29
28
30
private static void CanExecuteEventHandler ( Interactive control , CanExecuteRoutedEventArgs args )
@@ -46,6 +48,11 @@ private static void ExecutedEventHandler(Interactive control, ExecutedRoutedEven
46
48
}
47
49
}
48
50
51
+ private static void GotFocusEventHandler ( Interactive control , GotFocusEventArgs args )
52
+ {
53
+ _inputElement = control as IInputElement ;
54
+ }
55
+
49
56
public static RoutedEvent < CanExecuteRoutedEventArgs > CanExecuteEvent { get ; } = RoutedEvent . Register < CanExecuteRoutedEventArgs > ( nameof ( CanExecuteEvent ) , RoutingStrategies . Bubble , typeof ( RoutedCommand ) ) ;
50
57
51
58
public bool CanExecute ( object parameter , IInputElement target )
@@ -60,7 +67,7 @@ public bool CanExecute(object parameter, IInputElement target)
60
67
61
68
bool ICommand . CanExecute ( object parameter )
62
69
{
63
- return CanExecute ( parameter , Application . Current . FocusManager . Current ) ;
70
+ return CanExecute ( parameter , _inputElement ) ;
64
71
}
65
72
66
73
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)
75
82
76
83
void ICommand . Execute ( object parameter )
77
84
{
78
- Execute ( parameter , Application . Current . FocusManager . Current ) ;
85
+ Execute ( parameter , _inputElement ) ;
79
86
}
80
87
81
88
// TODO
0 commit comments