title: WPF MVVM KeyBinding not being recognized right away and not always working
body: <p>For whatever reason, the KeyBindings for my UserControl aren't working as soon as my WPF Application loads. They do work after I press a button on the form but not when I set focus to the form by clicking or alt tabbing or moving or anything like that. And when they do work my enter keys print a random number. (sometimes 5, sometimes 7 etc...). </p>
  
  <pre><code>&lt;UserControl x:Class="WpfCalculator.View.CalculatorView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Height="300" Width="300"
      &gt;
  &lt;UserControl.InputBindings&gt;
      &lt;KeyBinding Key="DELETE" Command="{Binding Path=IBackspaceOnInput}" /&gt;
      &lt;KeyBinding Key="BACKSPACE" Command="{Binding Path=IBackspaceOnInput}" /&gt;
  
      &lt;KeyBinding Key="NUMPAD0" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" /&gt;
      &lt;KeyBinding Key="NUMPAD1" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" /&gt;
      &lt;KeyBinding Key="NUMPAD2" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" /&gt;
      &lt;KeyBinding Key="NUMPAD3" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" /&gt;
      &lt;KeyBinding Key="NUMPAD4" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" /&gt;
      &lt;KeyBinding Key="NUMPAD5" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" /&gt;
      &lt;KeyBinding Key="NUMPAD6" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" /&gt;
      &lt;KeyBinding Key="NUMPAD7" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" /&gt;
      &lt;KeyBinding Key="NUMPAD8" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" /&gt;
      &lt;KeyBinding Key="NUMPAD9" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /&gt;
  
      &lt;KeyBinding Key="D0" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" /&gt;
      &lt;KeyBinding Key="D1" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" /&gt;
      &lt;KeyBinding Key="D2" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" /&gt;
      &lt;KeyBinding Key="D3" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" /&gt;
      &lt;KeyBinding Key="D4" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" /&gt;
      &lt;KeyBinding Key="D5" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" /&gt;
      &lt;KeyBinding Key="D6" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" /&gt;
      &lt;KeyBinding Key="D7" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" /&gt;
      &lt;KeyBinding Key="D8" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" /&gt;
      &lt;KeyBinding Key="D9" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /&gt;
  
      &lt;KeyBinding Key="ADD" Command="{Binding Path=IAppendToUserInput}" CommandParameter="+" /&gt;
      &lt;KeyBinding Key="SUBTRACT" Command="{Binding Path=IAppendToUserInput}" CommandParameter="-" /&gt;
      &lt;KeyBinding Key="MULTIPLY" Command="{Binding Path=IAppendToUserInput}" CommandParameter="*" /&gt;
      &lt;KeyBinding Key="DIVIDE" Command="{Binding Path=IAppendToUserInput}" CommandParameter="/" /&gt;
  
      &lt;KeyBinding Key="Return" Command="{Binding Path=ICalculateExpression}" CommandParameter="" /&gt;
      &lt;KeyBinding Key="Enter" Command="{Binding Path=ICalculateExpression}" CommandParameter="" /&gt;
      &lt;KeyBinding Key="Escape" Command="{Binding Path=IClearInput}" CommandParameter="" /&gt;
  
      &lt;KeyBinding Gesture="CTRL+M" Command="{Binding Path=IRememberExpression}" CommandParameter="" /&gt;
      &lt;KeyBinding Gesture="CTRL+R" Command="{Binding Path=IRecallExpression}" CommandParameter="" /&gt;
      &lt;KeyBinding Gesture="CTRL+X" Command="{Binding Path=IForgetExpression}" CommandParameter="" /&gt;
  
      &lt;KeyBinding Key="Left" Command="{Binding Path=IMMoveCursor}" CommandParameter="1" /&gt;
      &lt;KeyBinding Key="Right" Command="{Binding Path=IMMoveCursor}" CommandParameter="-1" /&gt;
  
  &lt;/UserControl.InputBindings&gt;
  
  &lt;Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="305" Width="489"&gt;
      &lt;TextBox Name="input" HorizontalAlignment="Right"  IsReadOnly="True" Margin="0,12,200,271" Text="{Binding Path=UserInput}" Width="275" /&gt;
      &lt;Button Content="CE" Margin="143,0,323,147" Command="{Binding Path=IBackspaceOnInput}" CommandParameter="" Height="23" VerticalAlignment="Bottom" /&gt;
      &lt;Button Content="1" Height="23" HorizontalAlignment="Right" Margin="0,106,294,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="1" /&gt;
      &lt;Button Content="2" Height="23" HorizontalAlignment="Right" Margin="0,106,265,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="2" /&gt;
      &lt;Button Content="3" Height="23" HorizontalAlignment="Right" Margin="0,106,236,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="3" /&gt;
      &lt;Button Content="4" Height="23" HorizontalAlignment="Right" Margin="0,77,294,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="4" /&gt;
      &lt;Button Content="5" Height="23" HorizontalAlignment="Right" Margin="0,77,236,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="5" /&gt;
      &lt;Button Content="6" Height="23" HorizontalAlignment="Right" Margin="0,77,265,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="6" /&gt;
      &lt;Button Content="7" Height="23" HorizontalAlignment="Right" Margin="0,48,294,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="7" /&gt;
      &lt;Button Content="8" Height="23" HorizontalAlignment="Right" Margin="0,48,265,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="8" /&gt;
      &lt;Button Content="9" Height="23" HorizontalAlignment="Right" Margin="0,48,236,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /&gt;
      &lt;Button Content="0" Height="23" HorizontalAlignment="Left" Margin="201,135,0,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="0" /&gt;
  
      &lt;Button Content="+" Height="23" HorizontalAlignment="Right" Margin="0,48,201,234" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="+" /&gt;
      &lt;Button Content="-" Height="23" HorizontalAlignment="Right" Margin="0,77,201,205" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="-" /&gt;
      &lt;Button Content="*" Height="23" HorizontalAlignment="Right" Margin="0,106,201,176" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="*" /&gt;
      &lt;Button Content="/" Height="23" HorizontalAlignment="Right" Margin="0,135,201,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="/" /&gt;
      &lt;Button Content="." Height="23" HorizontalAlignment="Right" Margin="0,135,236,147" Width="23" Command="{Binding Path=IAppendToUserInput}" CommandParameter="9" /&gt;
  
      &lt;Button Content="C" HorizontalAlignment="Right" Margin="0,164,323,118" Width="23" Command="{Binding Path=IClearInput}" CommandParameter="" /&gt;
      &lt;Button Content="MC" Height="23" HorizontalAlignment="Right" Margin="0,0,323,234" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IForgetExpression}" CommandParameter=""/&gt;
      &lt;Button Content="M+" Height="23" HorizontalAlignment="Right" Margin="0,0,323,176" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IRememberExpression}" CommandParameter=""/&gt;
      &lt;Button Content="MR" Height="23" HorizontalAlignment="Right" Margin="0,0,323,205" VerticalAlignment="Bottom" Width="23" Command="{Binding Path=IRecallExpression}" CommandParameter=""/&gt;
  
      &lt;Expander ExpandDirection="Left" Header="History" Height="91" HorizontalAlignment="Right" Margin="0,193,200,0" VerticalAlignment="Top" Width="275"&gt;
          &lt;ListView ItemsSource="{Binding Path=History}" &gt;
              &lt;ListView.View&gt;
                  &lt;GridView &gt;
                      &lt;GridViewColumn Header="Start" DisplayMemberBinding="{Binding Started}"/&gt;
                      &lt;GridViewColumn Header="End" DisplayMemberBinding="{Binding Completed}"/&gt;
                      &lt;GridViewColumn Header="Expression" DisplayMemberBinding="{Binding Calculation}"/&gt;
                      &lt;GridViewColumn Header="Solution" DisplayMemberBinding="{Binding Result}"/&gt;
                  &lt;/GridView&gt;
              &lt;/ListView.View&gt;
          &lt;/ListView&gt;
      &lt;/Expander&gt;
      &lt;Button Command="{Binding Path=ICalculateExpression}" CommandParameter="" IsEnabled="{Binding Path=IsEqualsBtnEnabled}" Content="=" Height="23" Margin="172,0,236,118" VerticalAlignment="Bottom" /&gt;
  
  &lt;/Grid&gt;&lt;/UserControl&gt;
  </code></pre>
  
  <p>I really haven't come across anyone else with this particular problem so I'm not really sure what to give any more than this. Let me know if there is any other info required?</p>