Checkboxes only in Children Nodes?

2020-04-08 07:35发布

问题:

I have many items (Nodes) grouped by category and I want to display them in a TreeView where parent are text and children are CheckBoxes :

+-CategoryA(Text)
---.A1(CheckBox)
---.A2(CheckBox)
+-CategoryB(Text)
---.B1
---.B2
---.B3
+-CategoryC(Text)
---.C1(CheckBox)
---.C2(CheckBox)

Here is a code sample :

TreeNode testNodeA = new TreeNode("A"); 
TreeNode testNodeB = new TreeNode("B");
TreeNode testNodeC = new TreeNode("C");
TreeNode[] array = new TreeNode[] { testNodeA, testNodeB, testNodeC };
TreeNode cat = new TreeNode("Categorie X", array);
myTreeView.Nodes.Add(cat);

I already found a similar question Here, but the solution is not elegant since it uses p/invoke to call a C++ code.

回答1:

There might be an easier way; but you can do it by setting the draw mode to OwnerDrawAll and deciding what to render.

There's a good example of almost exactly what you want here: http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/947aaded-6372-4253-8799-2b595f4a39b9/