方法1:直接在设计页面修改树形图属性。
绑定ImageList、ImageIndex/imagekey 、SelectedImageIndex/SelectedImageKey、checkBoxs、StateImageList。
如图,默认状态下,图标为 ,选中状态下,图标为 。复选框未选中时图标为 ,目标选中时,图标为。
对应的页面的*.Designer.cs代码(以ImageIndex和SelectedImageIndex为例):
this.treeView_main.CheckBoxes = true; //是否复选框
this.treeView_main.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView_main.ImageIndex = 0; //默认图标索引
this.treeView_main.ImageList = this.imageList1; //默认图标绑定图片列控件
this.treeView_main.Location = new System.Drawing.Point(0, 0);
this.treeView_main.Name = "treeView_main";
this.treeView_main.SelectedImageIndex = 1; //选中图标对应索引
this.treeView_main.Size = new System.Drawing.Size(200, 450);
this.treeView_main.StateImageList = this.imageList_State; //自定义状态(复选框是否选中)状态绑定图片控件,(默认第0张,选中第1张)
this.treeView_main.TabIndex = 0;
对应的页面的*.Designer.cs代码(以ImageKey和SelectedImageKey为例):
this.treeView_main.CheckBoxes = true; //是否复选框
this.treeView_main.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView_main.ImageKey = "tree_add.png"; //默认图标名称
this.treeView_main.ImageList = this.imageList1; //默认图标绑定图片列控件
this.treeView_main.Location = new System.Drawing.Point(0, 0);
this.treeView_main.Name = "treeView_main";
this.treeView_main.SelectedImageKey = "tree_plus.png"; //选中图标名称
this.treeView_main.Size = new System.Drawing.Size(200, 450);
this.treeView_main.StateImageList = this.imageList_State; //自定义状态(复选框是否选中)状态绑定图片控件,(默认第0张,选中第1张)
this.treeView_main.TabIndex = 0;
方法2:修改后台代码
后台代码如下: