寻找带有复选框的 WPF ComboBox
我的谷歌技能让我失败了。 任何人都听说过 WPF 这样的控件。 我试图让它看起来像这样(winforms 屏幕截图)。
My google skills fail me. Anyone heard of a control like that for WPF. I am trying to make it look like this (winforms screenshot).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过设置组合框的 DataTemplate 自行完成此操作。 本文向您展示了如何操作 - 对于列表框,但原则是相同的。
另一篇文章 here 也许更适合您想要做的事情,只需将项目模板的第一列设置为复选框并将其绑定到业务对象上的布尔值即可。
You can do this yourself by setting the DataTemplate of the combo box. This article shows you how - for a listbox, but the principle is the same.
Another article here is perhaps a better fit for what you are trying to do, simple set the first column of the item template to be a checkbox and bind it to a bool on your business object.
这是我的组合框。 我使用 Martin Harris 代码和此链接中的代码 当选择为空时,WPF ComboBox 可以显示替代文本吗?
数据源小类:
有两个处理程序 - 一个用于处理单击的 CheckBox,一个用于为 ComboBox 形成文本。
对于 ComboBox.ItemsSource,我使用
其中 tblObject 是我的对象的类型,我想在 ComboBox 中显示其列表。
我希望这段代码对某人有用!
There is my combobox. I use Martin Harris code and code from this link Can a WPF ComboBox display alternative text when its selection is null?
Small class for datasource:
And there is two handler - one for handling CheckBox clicked and one for forming Text for ComboBox.
For ComboBox.ItemsSource I use
where tblObject is type of my object, a list of which I want to display in ComboBox.
I hope this code is useful to someone!
尝试一下 CheckComboBox 来自 扩展 WPF 工具包。
对我来说,主要优点是有两个用于绑定的列表:
我发现这种方法更实用。 此外,您还可以指定要绑定的集合的
value
和display
成员。如果您不想使用
CheckComboBox
带来一堆其他控件,您可以获取 源代码,非常简单(需要带上源代码) com/xceedsoftware/wpftoolkit/blob/master/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Primitives/Selector.cs" rel="nofollow noreferrer">选择器类)。Give a try to CheckComboBox from Extended WPF Toolkit.
The main advantage for me is having two lists for binding:
I find this approach more practical. In addition you can specify
value
anddisplay
members of the collections you're binding.If you don't want to bring a bunch of other controls with
CheckComboBox
, you can get the source code of it, it's pretty straightforward (need to bring Selector class as well).带有复选框的组合框
令人惊讶的是,所提供的答案对我不起作用,我尝试了许多变体,并不断收到有关复选框不属于组合框的错误消息,并且数据上下文似乎已损坏。
最后,我不需要做任何涉及数据模板或任何背后代码的事情,并且我的绑定工作正常(示例中未显示)
我必须说,在阅读了所有答案后,我很高兴发现这是多么容易。
ComboBox with Checkboxes
The provided answers surprisingly didn't work for me, I tried many variations and kept getting error messages about the checkbox not being part of combobox and the data context seemed to be broken.
In the end I didn't have to do anything involving data templates or any code behind and my bindings are working fine (not shown in example)
I must say I'm happy with how easy this turned out to be after reading all the answers.