WPF ComboBox with editable textbox as an item
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.
Sandeep Bansal
Updated on June 13, 2022Comments
-
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 almost 13 yearsThanks, I had no idea it was that easy, and there I was confused in what I needed to do. Thanks again
-
H.B. almost 13 yearsYou'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 theComboBox.Items
collection will still directly expose the content instead of the ComboBoxItem if you let the ComboBox create this container for you.