title: Validation in textbox in WPF
body: <p>I am currently working on a WPF application where I would like to have a <code>TextBox</code> that can only have numeric entries in it. I know that I can validate the content of it when I lost the focus and block the content from being numeric, but in other Windows Form application, we use to totally block any input except numerical from being written down. Plus, we use to put that code in a separate dll to reference it in many places.</p>
  
  <p>Here is the code in 2008 not using WPF:</p>
  
  <pre><code>Public Shared Sub BloquerInt(ByRef e As System.Windows.Forms.KeyPressEventArgs, ByRef oTxt As Windows.Forms.TextBox, ByVal intlongueur As Integer)
      Dim intLongueurSelect As Integer = oTxt.SelectionLength
      Dim intPosCurseur As Integer = oTxt.SelectionStart
      Dim strValeurTxtBox As String = oTxt.Text.Substring(0, intPosCurseur) &amp; oTxt.Text.Substring(intPosCurseur + intLongueurSelect, oTxt.Text.Length - intPosCurseur - intLongueurSelect)
  
      If IsNumeric(e.KeyChar) OrElse _
         Microsoft.VisualBasic.Asc(e.KeyChar) = System.Windows.Forms.Keys.Back Then
          If Microsoft.VisualBasic.AscW(e.KeyChar) = System.Windows.Forms.Keys.Back Then
              e.Handled = False
          ElseIf strValeurTxtBox.Length &lt; intlongueur Then
              e.Handled = False
          Else
              e.Handled = True
  
          End If
      Else
          e.Handled = True
      End If
  </code></pre>
  
  <p>Is there an equivalent way in WPF? I wouldn't mind if this is in a style but I am new to WPF so style are a bit obscure to what they can or can't do.</p>