beta.blog

WPF – Set the Text of a ComboBox

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


Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!