0
votes
<Window.InputBindings>
    <KeyBinding Key="0" Command="{Binding ZeroCommand}"/>
    <KeyBinding Key="1" Command="{Binding OneCommand}"/>
    <KeyBinding Key="2" Command="{Binding TwoCommand}"/>
    <KeyBinding Key="3" Command="{Binding ThreeCommand}"/>
    <KeyBinding Key="4" Command="{Binding FourCommand}"/>
    <KeyBinding Key="5" Command="{Binding FiveCommand}"/>
    <KeyBinding Key="6" Command="{Binding SixCommand}"/>
    <KeyBinding Key="7" Command="{Binding SevenCommand}"/>
    <KeyBinding Key="8" Command="{Binding EightCommand}"/>
    <KeyBinding Key="9" Command="{Binding NienCommand}"/>
    <KeyBinding Key="OemPlus" Command="{Binding PlusCommand}"/>
    <KeyBinding Key="OemMinus" Command="{Binding MinusCommand}"/>
    <KeyBinding Key="OemBackslash" Command="{Binding DevideCommand}"/>
    <KeyBinding Key="Multiply" Command="{Binding MultiplyCommand}"/>
    <KeyBinding Key="Return" Command="{Binding EqualsCommand}"/>
</Window.InputBindings>

I was creating a basic calculator app when I ran into this issue. I am getting a compilation error '0' can not be used as a value for 'Key'. Numbers are not valid enumeration values. In fact OemMinus is the only command that is being bound.

I think could fix this by creating a InputBindingConfiguration class with a singleton, define the keys there and bind to those keys but I was wondering if there was a way of doing this purely in Xaml ?

1
You need to provide a valid value for the Key property. Valid values would be names of the enumeration values for the System.Windows.Input.Key enum type. So where you have e.g. "0", you need to instead have "D0". See the documentation for the rest of the value names that are valid.Peter Duniho
I personally find this question very relevant - while I was providing key values I was surprised that WPF autocomplete provides purely numerical options, I thought that was done through a value converter. Clearly it accepts only D- values for numerical keys.Tom Charles Zhang

1 Answers

1
votes

check the proper key binding below Link : WPFKeyBindings

Basic Keys

Key - Code Key(s)

A - Z The basic letter keys.

D0 - D9 The numeric keys from the main section of the keyboard. Space The space bar.

F1 - F24 The function keys.