Reputation: 7643
How do I make a combobox stay open after an item is selected in C#?
I want to do this because it is actually a comboBox with a checklist so I can select several items.
Upvotes: 6
Views: 6250
Reputation: 423
While I realize the question was intended for winforms, please allow me to contribute an answer for wpf seekers(as I got here, actually).
simply put something like this in the combo box.
<Grid>
<ComboBox Width="1" Height="1" IsDropDownOpen="{Binding ElementName=TButton,Path=IsChecked,Mode=TwoWay}">
<Grid Width="200" Height="400" Background="BlanchedAlmond" Margin="-5" IsHitTestVisible="True">
<Button Background="Transparent"></Button> <--------- This is the equivalent of a e.handled
<StackPanel>
<Button Content="Button 1"/>
<Button Content="Button 1"/>
<Button Content="Button 1"/>
<Button Content="Button 1"/>
</StackPanel>
</Grid>
</ComboBox>
<ToggleButton x:Name="TButton" Content="Drop" MaxHeight="40" MaxWidth="40"/>
</Grid>
Quick and dirty fix for a decent, hassle free drop down control.
When you click the combobox item, you actually click the invisible button and the combo stays open.
I've tried with a hittest visible rect, but no dice. This does the trick.
Note
One should replace the invisible background with a style that completely strips the button (when mouse is over or a click is performed).
Also, a drop font icon / path instead of drop would be nice. :)
I apologize again for posting here. I've been looking all over the place for a quick drop down control with 0 outside dependencies(popup won't do) and all I've stumbled across are ridiculously (needlessly) complicated implementations. I hope this will steer somebody in the right direction!
Best of luck to you!
Edit A simple opacity 0 will do. Doh...
Upvotes: 1
Reputation: 6699
This functionality is available in .NET 3.0 and beyond. Use the ComboBox.StaysOpenOnEdit property
Upvotes: 1
Reputation: 75276
Use a ListBox
instead of a ComboBox. What you want to do (keep a ComboBox open even after the user has selected a choice) would be unusual and unexpected behavior.
Update: I think this sample will let you do what you want (which is to have a multi-select list that only takes up the form real estate of one control):
http://www.codeguru.com/csharp/csharp/cs_controls/treeview/article.php/c15373/
Upvotes: 2
Reputation: 12552
If you still want to allow the user to write an option of his own you can still use a DropBox, just set the DropDownStyle property to simple. You'll get something similar with a ListBox but with an TextBox on the top where the user can write somwthing.
Upvotes: 4