0

I have some validation code that uses masking and the PreviewTextInput and PreviewKeyDown events on a textbox. When I change the value in the textbox manually the validation works perfectly. When I set the values programatically the valdation doesn't start until I click in the box and delete a character and re-add it, manually firing one or both of the above events.

Is there a way to fire either of those events manually so that the validation will work?

I've tried stuff like:

this.TextBox.RaiseEvent(this.TextBox.PreviewTextInput);

But nothing seems to work. I can't get the types to match either. Any ideas are welcome.

The masking-based validation code I'm using can be seen here: How to define TextBox input restrictions?

Community
  • 1
  • 1
Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72
  • I'm not sure what you are trying to achieve, but you can modify the Text binding mode to [UpdateSourceTrigger=Explicit](http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx). That way you can exactly tell WPF when to update the source, which *should* also update the validation. But why don't you use the built in validation? – dowhilefor May 31 '12 at 15:18
  • I don't think faking a textbox event is the right way to solve the issue here. – Terry May 31 '12 at 15:20
  • I think it is, unless you have another suggestion. I'm not using real validation. I'm using masking. My code is here: http://stackoverflow.com/questions/1103765/how-to-define-textbox-input-restrictions/10759912#10759912 – Nathan Tornquist May 31 '12 at 16:00

3 Answers3

3

To complete Trevor Elliott's answer, I had to reuse the same TextCompositionEventArgs reference to make it work :

    TextCompositionEventArgs eventArgs =
      new TextCompositionEventArgs(
        InputManager
          .Current
          .PrimaryKeyboardDevice,
        new TextComposition(
          InputManager.Current, 
          target, 
          ((char) KeyInterop.VirtualKeyFromKey(KBB.Key)).ToString()));

    eventArgs.RoutedEvent = TextCompositionManager.PreviewTextInputStartEvent;
    target.RaiseEvent(eventArgs);

    eventArgs.RoutedEvent = TextCompositionManager.TextInputStartEvent;
    target.RaiseEvent(eventArgs);

    eventArgs.RoutedEvent = TextCompositionManager.PreviewTextInputEvent;
    target.RaiseEvent(eventArgs);

    eventArgs.RoutedEvent = TextCompositionManager.TextInputEvent;
    target.RaiseEvent(eventArgs);

Hope this helps

Axel Samyn
  • 160
  • 1
  • 12
0

You can use the following code to fake text input to a TextBox.

TextCompositionEventArgs args = new TextCompositionEventArgs(
    InputManager.Current.PrimaryKeyboardDevice,
    new TextComposition(InputManager.Current, txtBox, "Test text")
);
args.RoutedEvent = TextCompositionManager.PreviewTextInputEvent;

txtBox.RaiseEvent(args);

args = new TextCompositionEventArgs(
    InputManager.Current.PrimaryKeyboardDevice,
    new TextComposition(InputManager.Current, txtBox, "Test text")
);
args.RoutedEvent = TextCompositionManager.TextInputEvent;

txtBox.RaiseEvent(args);
Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
0

Or you could just select the text in the textbox after you enter the text via code eg t.Select().

In this way once the user moves focus off the text box, validation will fire.