Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
<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 ?

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 Jan 10, 2018 at 7:38 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 Aug 18, 2019 at 13:34

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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.