How do I get TextChanged events from an editable ComboBox

Wpf

Wpf Problem Overview


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.

Wpf Solutions


Solution 1 - Wpf

<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.)

Solution 2 - Wpf

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.

Solution 3 - Wpf

PreviewTextInput event gets triggered for every keyboard input in the ComboBox.

Solution 4 - Wpf

Add --->> TextBoxBase.TextChanged="ComboBox_TextChanged"

Solution 5 - Wpf

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" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser380719View Question on Stackoverflow
Solution 1 - WpfIanRView Answer on Stackoverflow
Solution 2 - WpfMathiasView Answer on Stackoverflow
Solution 3 - WpfGirish ReddyvariView Answer on Stackoverflow
Solution 4 - Wpfuser7077890View Answer on Stackoverflow
Solution 5 - WpfAlanView Answer on Stackoverflow