title: Custom Control in WPF: Creating Numeric PasswordBox
body: <p>I've created a numeric TextBox by inheriting the TextBox as follows:</p>
  
  <pre><code>public class NumericTextBox : TextBox
  {
      public NumericTextBox() : base()
      {
          this.AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(this.TextBoxPasting));
      }
  
      protected override void OnPreviewTextInput(TextCompositionEventArgs e)
      {
          base.OnPreviewTextInput(e);
          e.Handled = !IsTextAllowed(e.Text);
      }
  
      private bool IsTextAllowed(string text)
      {
          Regex regex = new Regex("[^0-9]+");
          return !regex.IsMatch(text);
      }
  
      private void TextBoxPasting(object sender, DataObjectPastingEventArgs e)
      {
          if (e.DataObject.GetDataPresent(typeof(String)))
          {
              String text = (String)e.DataObject.GetData(typeof(String));
              if (!IsTextAllowed(text))
              {
                  e.CancelCommand();
              }
          }
          else
          {
              e.CancelCommand();
          }
      }
  }
  </code></pre>
  
  <p>but i cannot do the same for PasswordBox as password box is a sealed class.
  Any idea how can i achieve this for a PasswordBox?</p>