如何控制 .Net Forms.TreeView 中的复选框?
我有一个 .Net 桌面应用程序,其中 TreeView 作为 UI 元素之一。
我希望能够多重选择该 TreeView,但根本不支持这一点。
所以我在树中添加了复选框,我的问题是只有一些项目是可选的,而那些不能选择的项目则不能一致地级联选择。
有什么方法可以在显示其他复选框时禁用或隐藏某些复选框吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近写的一个编辑器也遇到了非常类似的问题。 最后,我使用了
TreeNode
的BackColor
属性来确定节点的选择状态。 然后,我为SelectionChanged
事件编写了一个处理程序,该处理程序检查 Shift/Control 键的状态,以确定所选节点是否被添加到选择中/从选择中删除或创建新选择。 还有当前选择的节点的Generic::List
以消除任何树搜索。I had a very similar problem in an editor I wrote recently. In the end, I used the
TreeNode
'sBackColor
property to determine the selection state of the node. I then wrote a handler for theSelectionChanged
event that checked the state of the Shift/Control keys to determine if the selected node was being added to/removed from the selection or creating a new selection. There was also aGeneric::List<>
of the nodes that were currently selected to eliminate any tree searches.TreeView 的默认行为是,当 Checkboxes 属性设置为 true,则将为所有 TreeNode 显示复选框。
您正在寻找的行为 - 禁用或隐藏某些复选框 - 是正常行为的自定义修改。 您可以创建覆盖默认行为的 TreeView 或 TreeNode 的自定义实现。 另一件可以尝试的事情是使用 TreeView。StateImageList 属性。 要了解更多信息,请查看一些网站:
The default behavior of TreeView is that when the Checkboxes property is set to true, that checkboxes will be shown for all TreeNodes.
The behavior that you are looking for - to disable or hide some checkboxes - is a custom modification of the normal behavior. You can create a custom implementation of TreeView or TreeNode that overrides the default behavior. One other thing to try would be to use the TreeView.StateImageList property. Some sites to check out for more info:
MultiSelectTreeView:
MultiSelectTreeView: