c# - WinForms TreeView 中的三态复选框

标签 c# winforms treeview

我有一个 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);
    }
}

这将呈现:enter image description here

祝你好运!

关于c# - WinForms TreeView 中的三态复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626031/

相关文章:

javafx:如何在 TreeView 中隐藏 "drop down arrow"?

c# - 有没有办法让 WinForms DataGridView 的列显示记录中的多个值

c# - 使用 GMap.net 在 map 上绘制折线

php - MPTT树到菜单

c# - 使用 WebBrowser 控件时如何获得网站图标?

c# - 有没有办法通过 VS 设计器严格将控件颜色设置为十六进制值?

c# - 绑定(bind)到 WPF 中的 TreeView

c# - Action 的通用约束没有按预期工作

c# - 不在引用的程序集中键入 Foo,但我使用的是扩展类型 Foo 的类型 Bar

c# - C#中如何将数据表绑定(bind)到datagridview