且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c#.net中的treeview控件

更新时间:2023-11-04 23:44:10

一种方法是使用自定义ImageList,其中包含复选框图像(已选中和未选中)以及其他空白/自定义图像。



将TreeView上的CheckBoxes属性设置为True。定义如下课程:



One way is to use a custom ImageList which contains the 'checkbox' images (checked and unchecked) and an additional blank/custom image.

Set the CheckBoxes property on the TreeView to True. Define a class as below:

public class NonCheckableTreeNode : TreeNode
{

}





使用自定义类型添加节点后,将其StateImageIndex设置为空白图片的索引(或其他情况)。



然后处理BeforeCheck事件在TreeView上检查节点是否属于自定义类型。

虽然只有在以编程方式添加项目时才会真正起作用。



Once you have added the node using the custom type, set its StateImageIndex to the index of the blank picture (or other in your case).

Then handle the BeforeCheck event on the TreeView and check if the node is of the custom type.
Although this would only really work if you are adding the items programatically.

private void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    if (e.Node.GetType() == typeof(NonCheckableTreeNode))
        e.Cancel = true;
}





希望这会有所帮助。



Hope this helps.