我有一个 TreeView,它允许用户通过选中或取消选中每个项目的复选框来选择分层数据的某些元素。目前,我使用 checkbox hiding technique from another question 在有子节点的节点上禁用该框,像这样:
☑ Node 1
☐ Node 2
• Node 3
☑ Node 3.1
☑ Node 3.2
• Node 4
☐ Node 4.1
☑ Node 4.2
但更好的解决方案是为父节点使用三态复选框,如下所示:
☑ Node 1
☐ Node 2
☑ Node 3
☑ Node 3.1
☑ Node 3.2
☒ Node 4
☐ Node 4.1
☑ Node 4.2
由于此功能在 Win32 中可用,我的问题是如何在不自己绘制框的情况下执行此操作(例如,作为用户绘制的控件或 using an image list )。我根本不熟悉 Win32 API;如何扩展上面链接的技术以在托管 TreeView 控件上启用三态复选框?
最佳答案
如果您正在考虑绘制混合复选框,此代码可能会对您有所帮助
class MixedCheckBox:Control
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(0, 0), Bounds,
Text, Font, false,
System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal);
}
}
这将呈现:
祝你好运!
关于c# - WinForms TreeView 中的三态复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626031/