wpf 当文本输入到文本框时,立即触发 TextChanged 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8912456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:14:40  来源:igfitidea点击:

Having TextChanged Event Fire Immediately as Text is Typed into TextBox

wpfeventstextboxtextchangedlost-focus

提问by thecoop

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

TextBox具有TextChanged事件的 wpf上,它似乎仅在焦点从文本框移开时触发;但不是输入单个字符。

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

是否有类似于在文本框中键入字符而不是焦点更改时立即TextChanged触发的事件?

回答by Aaron McIver

You can bind the Textproperty and make use of the UpdateSourceTrigger.

您可以绑定Text属性并使用UpdateSourceTrigger

UpdateSourceTrigger=PropertyChanged

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

通过将其设置为PropertyChanged,您将在每次文本更改时收到通知。

回答by H.B.

TextChangeddoesfire as soon as the text is changed.

TextChanged确实只要文本更改火灾。

(If you have a binding on Textthat is notthe same thing, it is completely independent from the event.)

(如果你有一个绑定,Text不是一回事,它完全独立于事件。)

回答by Bathineni

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

@Anron 答案是正确的,但我认为它仅在我们进行数据绑定(使用 MVVM)时才有效。

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

从@thecorp 的问题中,我了解到他正在尝试在代码隐藏文件中进行。

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

您可以利用文本框的“KeyDown”和“KeyUp”事件,或者如果您使用数据绑定 Aaron anwer 应该可以解决您的问题。