15

On a wpf TextBox that has an TextChanged event, it seems to only fires when focus is taken away from the textbox; but not as individual characters are typed in.

Is there an event similar to TextChanged that fires immediately when a character is typed into the textbox, rather than when focus changes?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
thecoop
  • 45,220
  • 19
  • 132
  • 189

3 Answers3

28

You can bind the Text property and make use of the UpdateSourceTrigger.

UpdateSourceTrigger=PropertyChanged

By setting it to PropertyChanged, you will get a notification each and every time the text changes.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • Plz help. I had the same issue, and on the debugger the bounded property seems to return the correct value, but the textbox is not refreshed with it. Our use case is that we reject the user's input, and want to leave the previous value. – ilans Jun 09 '16 at 09:33
  • 1
    This is compilable -> without quotes, like this: – sa.he Jan 05 '17 at 16:45
12

TextChanged does fire as soon as the text is changed.

(If you have a binding on Text that is not the same thing, it is completely independent from the event.)

H.B.
  • 166,899
  • 29
  • 327
  • 400
2

@Anron answer is correct but i think it works only when we are doing Data binding (using MVVM).

From @thecorp question what i have understood is he is trying to it in code behind file.

You can take advantage of "KeyDown" and "KeyUp" events of textbox or if you are using data binding Aaron anwer should resolve your issue.

Bathineni
  • 3,436
  • 18
  • 25
  • "works only when we are doing Data binding" => Works on all Data binding, not just under an MVVM situation. – ΩmegaMan Jul 24 '19 at 19:03