WPF ComboBox...how to set the .Text property?

20,797

If you're trying to set the text to the text of one of the items in the list it should work correctly.

If you're trying to set it to an item that isn't in the list then the IsEditable property needs to be true. You can set the IsReadOnly property to true if you don't actually want users typing into the combo box.

If none of these cases fit you, can you post the code you're using?

Share:
20,797
AngryHacker
Author by

AngryHacker

Updated on July 09, 2022

Comments

  • AngryHacker
    AngryHacker almost 2 years

    Is there a way to set the .Text property of the Wpf ComboBox control directly?

    My combobox is bound to a List<T> collection, but when I try to set .Text property in the DropDownClosed event, it totally ignores it.

  • modernzombie
    modernzombie over 13 years
    Thought I'd mention this as I just wasted a couple hours figuring it out. If the text you are trying to set is not on of the items in the list and you are using MultiBinding (i.e first + name name) and you set IsEditable="True" to make it work as Ray suggests when you select an item the text will display the Object Type (i.e. MY.Project.Person) instead of the proper text. I haven't come up with a solution yet.
  • SLaks
    SLaks almost 12 years
    @modernzombie: Override ToString() in the object.
  • Lee Louviere
    Lee Louviere almost 12 years
    @modernzombie Or, use ComboBoxItem, and set the Tag on the item to the actual object, and the content to the string you want to display.