title: WPF, create Custom DataGridTextColumn to Prevent unwanted Character
body: <p>i newbie to WPF,
  i want to prevent user to input character, eg. character "-", so 
  i created custom DataGridTextColumn with following code :</p>
  
  <pre><code>public class DataGridNumericColumn : DataGridTextColumn
  {
      protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
      {
          var textBox = (TextBox) editingElement;
          textBox.PreviewTextInput += OnPreviewTextInput;
          return base.PrepareCellForEdit(editingElement, editingEventArgs);
      }
  
  
      private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
      {
          var textBox = (TextBox)sender;
          if (e.Text == "-")
              return;
          if (!this.IsNumeric(e.Text))
              e.Handled = true;
      }
  }
  </code></pre>
  
  <p>and XAML :</p>
  
  <pre><code>&lt;ZF:ZFDataGrid
          Grid.Row="4" Grid.Column="0" 
          HorizontalAlignment="Stretch" VerticalAlignment="Top"
          HorizontalContentAlignment="Stretch"
          VerticalContentAlignment="Stretch"
          CanUserAddRows="True"
          CanUserDeleteRows="False"
          CanUserResizeRows="False"
          CanUserReorderColumns="False"
          CanUserSortColumns="False"
          IsSynchronizedWithCurrentItem="True"
          SelectionUnit="Cell"
          SelectionMode="Single"
          Margin="3,3,3,0" 
          AutoGenerateColumns="False"
          AlternatingRowBackground="WhiteSmoke"
          RowHeaderWidth="30"
          FontSize="18"
          ItemsSource="{Binding POSModel}"&gt;
      &lt;ZF:DataGridNumericColumn Header="Qty" Width="80" /&gt;
  &lt;/ZF:ZFDataGrid&gt;
  </code></pre>
  
  <p>the Custom DataGridNumericColumn work well, except when i press the character for the first time.
  if i press F2 to edit or double click the column and then press the key, everything works well.</p>
  
  <p>but if i press the key without editing the cell first, the custom DataGridNumericColumn  not work.</p>
  
  <p>i put breakpoint on PrepareCellForEdit, and the coding works. but method OnPreviewTextInput works the second time when i press the key. not the first one.</p>
  
  <p>can anyone give me another solution ?</p>
  
  <p>EDITED:</p>
  
  <pre><code>protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
      {
          var textBox = (TextBox) editingElement;
          textBox.PreviewTextInput += OnPreviewTextInput;
          textBox.TextChanged += OnTextChanged; //change here
          return base.PrepareCellForEdit(editingElement, editingEventArgs);
      }
  </code></pre>
  
  <p>this code only run ONCE, the rest will be handled by OnPreviewTextInput</p>
  
  <pre><code>  private void OnTextChanged(object sender, TextChangedEventArgs e)
      {
          var textBox = (TextBox)sender;
  
          if (textBox.Text.Contains("-"))
          {
              textBox.TextChanged -= OnTextChanged;
              textBox.Text = "";
          }
      }
  </code></pre>