Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to set the TextBoxBase.TextChanged of a ComboBox via code. In other words, I want to do the following:

XML
<Combobox Textboxbase.Textchanged="comboBox1_TextChanged" />


...but outside of XAML (in C# code).
Can someone help me? I've tried some things and searched the web but I had no luck. I haven't delved in to WPF so such yet...

Perhaps I must do sth with System.Windows.Controls.Primitives.TextBoxBase.TextChangedEvent but I have no idea what and how...

I have found that I could access the inner TextBox of the ComboBox like that:

C#
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
        base.OnRender(drawingContext);
        TextBox txt = this.GetTemplateChild("PART_EditableTextBox") as TextBox;
        txt.TextChanged += new TextChangedEventHandler(txt_TextChanged);
}

...to set the event there, but isn't there an easier/better/reliable way?
Posted
Updated 1-Nov-12 22:52pm
v9
Comments
YvesDaoust 2-Nov-12 5:52am    
Doesn't it work if you just assign a value to the Text property and handle the TextChanged event of the Combo ?
KEL3 2-Nov-12 6:08am    
Unfortunately not.
This is mostly a problem for ComboBoxes with the IsEditable property set to true.
There is a problem with ComboBox and when the event handler for TextChanged gets called (when you edit the text contents of it by typing inside it) you cannot see the new Text value (you see the value before modification).

This is a related discussion:
http://social.msdn.microsoft.com/Forums/en/wpf/thread/bf7de95a-b088-48e9-989f-d5f9e3a857da

Textboxbase.Textchanged solves a lot of problems, but I have problems accessing it by code...
YvesDaoust 2-Nov-12 6:13am    
What is the real issue then, that you can't raise the event or that you can't get the changed content ?
KEL3 2-Nov-12 6:27am    
Oups! My bad!
I CAN get the new value (I just do (e.OriginalSource as TextBox).Text). Forget what I said before.
The problem is how to set the event handler by code.
There doesn't seem to be any comboBox1.TextChanged.
YvesDaoust 2-Nov-12 9:45am    
You are right. I was referring to the Windows Forms combo. In WPF, you will use property comboBox1.TextBox.Text and event comboBox1.TextBox.TextChanged

As a workaround, you can bind an auxiliary TextBox to the ComboBox so that it reflects the text. Then attach your handler to the TextBox TextChanged event and make the TextBox invisible.
<combobox height="23" horizontalalignment="Left" margin="98,53,0,0" name="comboBox1" verticalalignment="Top" width="120" iseditable="True" />
<textbox height="23" horizontalalignment="Left" margin="100,95,0,0" name="textBox1" verticalalignment="Top" width="120">Text="{Binding ElementName=comboBox1, Path=Text}" TextChanged="textBox1_TextChanged" Visibility="Hidden" /></textbox>

Works fine for me.
 
Share this answer
 
v4
Comments
Akinmade Bond 2-Nov-12 13:03pm    
This would work.
Clifford Nelson 2-Nov-12 14:59pm    
Somebody screwed up the solution
KEL3 3-Nov-12 9:25am    
Thank you all.
Finally, I decided to go with the OnRender() approach and add a property in a derived class that returns the inner TextBox (as MS should have done). It saves memory and complexity (I think).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900