Reputation: 9903
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.
Upvotes: 70
Views: 56432
Reputation: 326
I would like to thank the answer from IanR and Girish Reddyvari as it got me thinking.
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
TextBoxBase.TextChanged
as my background in xaml and interactivity isn't good enough! But it did pick up
KeyUp
My code using Caliburn Micro is different but the following code should also work
<ComboBox IsEditable="True" KeyUp="ComboBox_TextChanged" />
Upvotes: 0
Reputation: 243
PreviewTextInput event gets triggered for every keyboard input in the ComboBox.
Upvotes: 3
Reputation: 281
Based on the approach above I had a look into the (XAML) generated code.
<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.
Upvotes: 26
Reputation: 4773
<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?)
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.
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).
(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.)
Upvotes: 154