WPF 在 XAML 中添加带有变音字符的 KeyBinding 事件

WPF Add KeyBinding events with umlaut characters in XAML

谁能告诉我为什么键绑定事件不适用于像这样的变音字符?

正在尝试 xaml

<Grid.InputBindings>
    <KeyBinding Key="Ü" ... />
</Grid.InputBindings>

会抛出错误:

Cannot convert string value 'Ü' to type System.Windows.Input.Key

因为KeyBinding.Key是枚举,所以可以看到所有可能的值here。由于您的元音变音字符不是该枚举的一部分 - 您不能使用它。

我做了一个小测试程序来找出 Key 是什么。创建新的 WPF 项目并添加到主 window cs 文件:

public MainWindow()
{
    InitializeComponent();
    var skip = new[] { Key.None, Key.DeadCharProcessed };
    foreach (Key value in Enum.GetValues(typeof(Key)))
        if (!skip.Contains(value))
            InputBindings.Add(new KeyBinding { Command = new MyCommand(value.ToString()), Key = value });
}

public class MyCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public string Text { get; }

    public MyCommand(string text) { Text = text; }

    public bool CanExecute(object parameter) => true;
    public void Execute(object parameter) => MessageBox.Show(Text);
}

为您节省时间:

Ü = Key.Oem1
Ö = Key.Oem3
Ä = Key.OemQuotes