title: How to force textbox to take only numbers in WPF?
body: <p>I want user to enter only numeric values in <code>TextBox</code>.</p>
  
  <p>I got this code:</p>
  
  <pre><code>private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
  {
       int isNumber = 0;
       e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
  }
  </code></pre>
  
  <p>But I am not getting <code>textbox_KeyPress</code> event and <code>e.KeyChar</code> while using WPF.</p>
  
  <p>Whats the solution in WPF?</p>
  
  <p><strong><code>Edit:</code></strong></p>
  
  <p>I made a Solution! </p>
  
  <pre><code>private void txtName_PreviewTextInput(object sender, TextCompositionEventArgs e)
  {
      CheckIsNumeric(e);
  }
  
  private void CheckIsNumeric(TextCompositionEventArgs e)
  {
      int result;
  
      if(!(int.TryParse(e.Text, out result) || e.Text == "."))
      {
          e.Handled = true;
      }
  }
  </code></pre>