如何从可编辑的 ComboBox 获取 TextChanged 事件 [英] How do I get TextChanged events from an editable ComboBox

查看:57
本文介绍了如何从可编辑的 ComboBox 获取 TextChanged 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编辑的组合框:

I have an editable ComboBox:

<ComboBox IsEditable="true"/>

编辑值改变时引发的事件是什么?我尝试过 TextInput,但这不是解决方案.

What is the event that is raised when the edited value is changed? I have tried TextInput but that is not the solution.

推荐答案

<ComboBox IsEditable="True" TextBoxBase.TextChanged="ComboBox_TextChanged" />

...应该这样做.(假设您想要在每次对文本进行更改时触发的内容,而不是在用户完成输入文本时触发.在这种情况下,您需要另一个事件 - 可能是 LostFocus 事件或其他什么?)

...should do it. (Assuming you want something that will fire every time a change is made to the text, rather then when the user has finished entering the text. In which case you'd need another event - maybe a LostFocus event or something?)

无论如何,上述 XAML 之所以有效,是因为当 IsEditable 设置为 true 时,ComboBox 使用 TextBox 来显示和编辑文本.TextBox 的 TextChanged 事件是一个 冒泡事件 - 意味着它会冒泡向上遍历元素树,以便我们可以在 ComboBox 本身上处理它.

Anyway, the reason why the above XAML works is that, when IsEditable is set to true, the ComboBox uses a TextBox for displaying and editing the text. The TextBox's TextChanged event is a bubbling event - meaning it will bubble up through the element tree so we can handle it on the ComboBox itself.

唯一的棘手"是 ComboBox 本身不公开 TextChanged 事件,但您仍然可以使用 附加事件(因此是 TextBoxBase.TextChanged 语法).

The only 'tricky' bit is that ComboBox doesn't expose a TextChanged event itself but you can still define a handler for it using an attached event (hence the TextBoxBase.TextChanged syntax).

(为了完整性可能值得注意,如果 ComboBox 碰巧包含多个 TextBox,那么只要其中任何一个文本框发生更改,就会调用处理程序.)

(It's probably worth noting for completeness, that if the ComboBox happened to contain more than one TextBox then the handler would be called whenever any of them had their text changed.)

这篇关于如何从可编辑的 ComboBox 获取 TextChanged 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆