在展开崩溃事件上更改TreeNode映像 [英] Change TreeNode image on expand-collapse events

查看:37
本文介绍了在展开崩溃事件上更改TreeNode映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有很多节点的treeView.我希望一些节点在折叠/展开时更改其图像.我该怎么办?

I have a treeView with many nodes. I want that some nodes change their image when node collapsed/expanded. How can I do it ?

不幸的是,TreeNode没有诸如ExpandNodeImage,CollapseNodeImage \

Unfortunately, TreeNode don't have properties like ExpandNodeImage, CollapseNodeImage \

TreeView可以经常更改,因此可以删除/添加节点.我可以删除子节点,等等...

TreeView can change very often, so nodes can be deleted/added.. i can delete child nodes and so on...

也许有像ExpandAndCollapseNode这样的类?

Maybe, there is a class like ExpandAndCollapseNode ?

推荐答案

1).将ImageList控件添加到WinForm.

1). Add an ImageList Control to your WinForm.

2).响应于用户在运行时使用TreeView所做的操作(例如扩展或折叠节点),在ImageList中填充您希望更改/显示的图片/图标.

2). Populate the ImageList with the pictures/icons you wish to change/display in response to what the user does at run-time with the TreeView, such as expanding, or collapsing nodes.

3).将'ImageList控件分配给'TreeView

3). Assign the 'ImageList Control to the 'ImageList property of the 'TreeView

这时,您可能希望在TreeView上进行初始传递(假设已填充),将Node.ImageIndex属性分配为指向要用于Node的ImageList中的Image....取决于它是否有孩子.

At this point you may want to make an initial pass over the TreeView, assuming it is populated, assigning the Node.ImageIndex property to point to the Image ... in the ImageList ... you want to use for the Node depending on whether it has children, or whatever.

4).例如,如果用户展开一个节点,则可以使用TreeView的BeforeExpand事件来更改该节点的图片:像这样:在这种情况下,我们使用ImageList在ImageList中的索引:

4). If a user expands a Node, for example, you can use the BeforeExpand Event of the TreeView to change the Node's picture : like so : in this case we use the index of the Picture in the ImageList :

    private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
    {
        e.Node.ImageIndex = 3;
    }

5)您还可以使用ImageKey属性设置节点的图像,该属性是Image的字符串名称

5) You can also set the Node's image by using the ImageKey property which is the string name of the Image

6)还有许多其他可能的Node Picture变体要使用:签出:SelectedImageIndex和SelectedImageKey:您还可以根据需要的效果在BeforeSelect,AfterSelect和BeforeExpand事件中更改Node图片.

6) There lots of other possible Node Picture variations to use : check out : SelectedImageIndex and SelectedImageKey : You can change Node pictures in the BeforeSelect, AfterSelect and BeforeExpand, events also, depending on the effect you are after.

这篇关于在展开崩溃事件上更改TreeNode映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆