Programming
WPF – Set the Text of a ComboBox
by admin on Feb.20, 2012, under Programming
I have got a ComboBox in one of my WPF projects. The relevant XAML-Code looks like this:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="141,100,0,0" Name="cbx_Subsequent" VerticalAlignment="Top" Width="156"> <ComboBoxItem>Take no action</ComboBoxItem> <ComboBoxItem>Restart service</ComboBoxItem> <ComboBoxItem>Reboot computer</ComboBoxItem> </ComboBox>
When executing the program the ComboBox initially doesn’t have a selection. I am therefore changing its text like this:
Changing the text to an item from my list:
this.cbx_Subsequent.Text = "Take no action";
Changing the text to something which is NOT in my list:
this.cbx_Subsequent.IsEditable = true; this.cbx_Subsequent.Text = "Take no action";
As you can see it is necessary to set IsEditable to true if you plan to set the text to something that is not in your list. Executing this code now changes the text of the Combo Box.