选择项目后如何使组合框保持打开状态?
在 C# 中选择某个项目后,如何使组合框保持打开状态?
我想这样做是因为它实际上是一个带有清单的组合框,因此我可以选择多个项目。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 ListBox 代替,因为
ComboBox
通过其核心设计来实现这种行为。ListBox
提供了类似的效果(因此它仍然打开)并支持多重选择。Use ListBox instead, since
ComboBox
does that behavior by its core design.ListBox
gives a similar effect (hence it is still open) and enabling for multi-selection.如果您仍然希望允许用户编写自己的选项,您仍然可以使用 DropBox,只需将 DropDownStyle 属性设置为 simple 即可。您将得到与列表框类似的内容,但顶部有一个文本框,用户可以在其中编写一些内容。
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.
使用
ListBox
而不是ComboBox。您想要做的事情(即使在用户选择了一个选项后仍保持组合框打开)将是不寻常和意外的行为。更新:我认为这个示例可以让您做您想做的事情(即拥有一个只占用一个控件的表单空间的多选列表):
http://www.codeguru.com/csharp/csharp/cs_controls/treeview/article.php /c15373/
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/
此功能在 .NET 3.0 及更高版本中可用。使用 ComboBox.StaysOpenOnEdit 属性
This functionality is available in .NET 3.0 and beyond. Use the ComboBox.StaysOpenOnEdit property
虽然我意识到这个问题是针对 winforms 的,但请允许我为 wpf 寻求者提供一个答案(实际上,正如我到达这里的那样)。
只需将类似的内容放入组合框中即可。
快速而肮脏的修复,以获得体面、无麻烦的下拉控制。
当您单击组合框项目时,您实际上单击了不可见的按钮,并且组合保持打开状态。
我尝试过使用最热门的可见矩形,但没有骰子。这可以解决问题。
注意
应将不可见背景替换为完全剥离按钮的样式(当鼠标悬停或执行单击时)。
另外,使用下拉字体图标/路径而不是drop会很好。 :)
我再次为在这里发帖道歉。我一直在到处寻找一个具有 0 个外部依赖项的快速下拉控件(弹出窗口不会这样做),而我偶然发现的都是可笑的(不必要的)复杂的实现。我希望这能引导某人走向正确的方向!
祝你好运!
编辑
简单的不透明度 0 就可以了。嚯……
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.
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...