wpf 如何从可编辑的 ComboBox 获取 TextChanged 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6914942/
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
How do I get TextChanged events from an editable ComboBox
提问by user380719
I have an editable ComboBox:
我有一个可编辑的组合框:
<ComboBox IsEditable="true"/>
What is the event that is raised when the edited value is changed? I have tried TextInput but that is not the solution.
更改编辑值时引发的事件是什么?我尝试过 TextInput 但这不是解决方案。
回答by IanR
<ComboBox IsEditable="True" TextBoxBase.TextChanged="ComboBox_TextChanged" />
...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?)
……应该这样做。(假设您希望每次更改文本时都会触发某些内容,而不是在用户完成输入文本时触发。在这种情况下,您需要另一个事件 - 也许是 LostFocus 事件或其他什么?)
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.
无论如何,上述 XAML 起作用的原因是,当 IsEditable 设置为 true 时,ComboBox 使用 TextBox 来显示和编辑文本。TextBox 的 TextChanged 事件是一个冒泡事件- 这意味着它将在元素树中冒泡,因此我们可以在 ComboBox 本身上处理它。
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 不公开 TextChanged 事件本身,但您仍然可以使用附加事件为其定义处理程序(因此使用 TextBoxBase.TextChanged 语法)。
(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 碰巧包含多个 TextBox,那么只要其中任何一个文本发生更改,就会调用处理程序。)
回答by Mathias
Based on the approach above I had a look into the (XAML) generated code.
基于上述方法,我查看了(XAML)生成的代码。
<ComboBox x:Name="myComboBox" IsEditable="True"/>
Add the following code to initialization:
将以下代码添加到初始化中:
myComboBox.AddHandler(System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent,
new System.Windows.Controls.TextChangedEventHandler(ComboBox_TextChanged));
This works fine for me, because I needed a reusable ComboBox (SQL-Server dropdown list) which encapsulates all behaviour.
这对我来说很好用,因为我需要一个可重用的 ComboBox(SQL-Server 下拉列表)来封装所有行为。
回答by Girish Reddyvari
PreviewTextInput event gets triggered for every keyboard input in the ComboBox.
组合框中的每个键盘输入都会触发 PreviewTextInput 事件。
回答by Girish Reddyvari
Add --->> TextBoxBase.TextChanged="ComboBox_TextChanged"
添加--->> TextBoxBase.TextChanged="ComboBox_TextChanged"
回答by Alan
I would like to thank the answer from IanR and Girish Reddyvari as it got me thinking.
我要感谢 IanR 和 Girish Reddyvari 的回答,因为它让我思考。
I'm using Caliburn Micro and I'm trying to get the input of the Editable ComboBox while they are typing. Caliburn Micro didn't easily pick up the event
我正在使用 Caliburn Micro 并且我正在尝试在他们打字时获取可编辑组合框的输入。Caliburn Micro 没有轻易接手这个事件
TextBoxBase.TextChanged
as my background in xaml and interactivity isn't good enough! But it did pick up
因为我在 xaml 和交互方面的背景还不够好!但它确实捡起来了
KeyUp
My code using Caliburn Micro is different but the following code should also work
我使用 Caliburn Micro 的代码不同,但下面的代码也应该有效
<ComboBox IsEditable="True" KeyUp="ComboBox_TextChanged" />