简体   繁体   中英

XAML KeyBinding 1,2,3

I decided to duplicate the buttons by pressing the keys on my page but getting an error like this:

"1" can not be used as a value for "Key". Numbers are not valid enumeration values.

 <Page.InputBindings>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn1,Path=Content}"
                Key="1"/>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn2,Path=Content}"
                Key="2"/>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn3,Path=Content}"
                Key="3"/>
    <KeyBinding Command="{Binding Btn_Click}" 
               CommandParameter="{Binding ElementName=btn4,Path=Content}"
                Key="4"/>
</Page.InputBindings>

Can I deceive the system?

You can use D1 , which is the enum value for the 1 key. See the Key Enumeration page for a list of all possible values.

<KeyBinding Command="{Binding SomeCommand}" Key="D1"/>

or just biniding:

<KeyBinding Command="{Binding SomeCommand}" Key="{Binding MyKey}"/>

VM:

public Key MyKey
{
    get => Key.D1;
}

this is because you can't do enumerator like:

enum MyEnum
{
    1,
    2,
    3
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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