title: Mvvm TextBox KeyDown Event
body: <p>I'd like to know how I can handle a KeyDown Event with MVVM in my ViewModel.</p>
  
  <p>I have a TextBox and when the user hits a key, which is not a number, the input shouldn't be allowed. I'd normally do it with Code behind like this (not full code, just an easy example):    </p>
  
  <pre><code>private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
  
      // Determine whether the keystroke is a number from the top of the keyboard.
      if (e.KeyCode &lt; Keys.D0 || e.KeyCode &gt; Keys.D9)
      {
          e.Handled = true;
      }
  }
  </code></pre>
  
  <p>Now I want to put this somehow in my ViewModel with a Command. I'm new to MVVM and I'm only using Bindings right now (which works fine :) ), but I don't know how to use a Command at all... </p>
  
  <p>My TextBox looks i.e. like this:</p>
  
  <pre><code>&lt;TextBox Text="{Binding MyField, Mode=TwoWay}"/&gt;
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>private string _myfield;
  public string MyField{
    get { return _myfield; }
    set { 
      _myfield= value;
      RaisePropertyChanged( ()=&gt;MyField)
    }
  }
  </code></pre>
  
  <p>But the setter will only be called, when I leave the TextBox + I don't have access to the entered Key.</p>