WPF ComboBox with editable textbox as an item

18,227

Solution 1

Not quite sure what the problem is, have you tried this:

<ComboBox>
    <ComboBoxItem>Other</ComboBoxItem>
    <TextBox>TextBox</TextBox>
</ComboBox>

If this is not what you want, please explain what exactly you need...


(The TextBox-item might be quite hard to select so giving it a label which can be clicked might be of interest)

<ComboBox>
    <ComboBoxItem>Normal Item</ComboBoxItem>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Other: " VerticalAlignment="Center"/>
        <TextBox>Enter text...</TextBox>
    </StackPanel>
</ComboBox>

Solution 2

Have you tried setting ComboBox.IsEditable = true? Then you'll need just one item and the ComboBox.Text property.

Share:
18,227
Sandeep Bansal
Author by

Sandeep Bansal

Updated on June 13, 2022

Comments

  • Sandeep Bansal
    Sandeep Bansal almost 2 years

    I'm looking to have a combo box with only two items:

    -----------
    |  Other.. |
    ------------
    |  TextBox |
    ------------
    

    Text Box representing a physical textbox that can be editable and Other.. being just a regular combobox item.

    Can someone help me out on how I will need to edit it.

    I have tried changing the Combbox.itemtemplate with a stackpanel and then adding a textbox, but it didn't show up and that also stops the chance of me having a regular combobox item in the control.

    Thanks in advance.

  • Sandeep Bansal
    Sandeep Bansal almost 13 years
    Thanks, I had no idea it was that easy, and there I was confused in what I needed to do. Thanks again
  • H.B.
    H.B. almost 13 years
    You're welcome, glad it helped :) As a side note, the ComboBox will create a ComboBoxItem around anything which is not a ComboBoxItem already, but the above syntax is not identical to <ComboBoxItem><TextBox.../></ComboBoxItem> as the ComboBox.Items collection will still directly expose the content instead of the ComboBoxItem if you let the ComboBox create this container for you.