简体   繁体   English

WPF在XAML中使用变音符添加KeyBinding事件

[英]WPF Add KeyBinding events with umlaut characters in XAML

Can anyone tell me why keybinding events do not work with umlaut characters like this? 谁能告诉我为什么键绑定事件对像这样的变音符号不起作用?

在此处输入图片说明

Trying in xaml 在XAML中尝试

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

Will throw error: 会抛出错误:

Cannot convert string value 'Ü' to type System.Windows.Input.Key 无法将字符串值“Ü”转换为System.Windows.Input.Key类型

I've made a small test program to find out which Key is what. 我编写了一个小型测试程序,以找出哪个Key是什么。 Make new WPF project and add to main window cs-file: 制作新的WPF项目并添加到主窗口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);
}

To save you time: 为节省时间:

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

Because KeyBinding.Key is enumeration, you can see all possible values here . 由于KeyBinding.Key是枚举,因此您可以在此处查看所有可能的值。 Since your umlaut character is not part of that enumeraton - you cannot use it. 由于您的变音符不是该枚举的一部分-您不能使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM