C# 如何为 TreeView 中的某些但不是所有节点设置图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/261660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:19:37  来源:igfitidea点击:

How do I set an image for some but not all nodes in a TreeView?

c#.netwinformstreeview

提问by Simon

I have a TreeViewwindows forms control with an ImageList, and I want some of the nodes to display images, but the others to not have images.

我有一个带有 的TreeViewwindows 窗体控件ImageList,我希望一些节点显示图像,但其他节点没有图像。

I don'twant a blank space where the image should be. I don'twant an image that looks like the lines that the TreeView would draw if it didn't have an ImageList. How do I get it to draw images for some items and not others, without resorting to clumsy hacks like that?

想要图像应该出现的空白区域。如果没有 ImageList,我想要看起来像 TreeView 将绘制的线条的图像。如何让它为某些项目而不是其他项目绘制图像,而不诉诸像这样的笨拙黑客?

采纳答案by Martin Brown

I tried this once and I don't think it is possible.

我试过一次,我认为这是不可能的。

If you try to set both ImageKeyand ImageIndexto "not set" values the control just defaults ImageIndexto 0. The following code:

如果您尝试同时设置ImageKeyImageIndex“未设置”值,则控件默认ImageIndex为 0。以下代码:

treeView.ImageKey = "Value";
Debug.WriteLine(treeView.ImageIndex);
treeView.ImageKey = null;
Debug.WriteLine(treeView.ImageIndex);
treeView.ImageIndex = -1;
Debug.WriteLine(treeView.ImageIndex);

Produces output:

产生输出:

-1
0
0

This kind of tells you that the control developers wanted to make sure that there was always a default image. That just leaves you with the hack options I'm afraid.

这种类型告诉您控件开发人员希望确保始终存在默认图像。恐怕这只会给你留下黑客选项。

回答by John Fischer

You need to set ImageIndexand SelectedImageIndexto a number that is higher than the number of values in your ImageList. For example, if you create this node and add it to your TreeView:

您需要将ImageIndex和设置SelectedImageIndex为一个大于ImageList. 例如,如果您创建此节点并将其添加到您的TreeView

TreeNode node1 = new TreeNode(string.Empty, 12, 12); // imageList1.Count = 5

you will have an invisible TreeNodeinserted into your TreeView. I changed the background color of my TreeViewand it was still invisible.

您将有一个隐形TreeNode插入到您的TreeView. 我改变了我的背景颜色,TreeView它仍然不可见。

(I googled this for some time, and I eventually found the answer here: http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.windowsforms/2006-09/msg00322.html)

(我用谷歌搜索了一段时间,最终在这里找到了答案:http: //www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.windowsforms/2006-09/msg00322.html

回答by user1887120

This will draw the TreeNodetext where the image should have been, getting rid of the white space.

这将TreeNode在图像应该出现的地方绘制文本,消除空白。

You'll need to set the TreeView's DrawModeproperty to OwnerDrawText. You can find the DrawModeproperty in the properties panel.

您需要将TreeViewDrawMode属性设置为OwnerDrawText。您可以DrawMode在属性面板中找到该属性。

Next when you add a node, set it's ImageIndexand SelectedImageIndexgreater than the value of your yourImageListName.Images.Countvalue. This is so no image will be drawn, but there will still be that white space you don't want.

接下来,当你添加一个节点,设置它的ImageIndexSelectedImageIndex比你的价值更大的yourImageListName.Images.Count价值。这样就不会绘制图像,但仍然会有您不想要的空白区域。

Now to get rid the white space. Add a handle for the treeviews DrawNodeevent. This can be done by going to the treeviews property panel and clicking the Icon in the panel that looks like a lighting bolt, then scroll till you see the text DrawNode, double click it.

现在摆脱空白。为 treeviewsDrawNode事件添加句柄。这可以通过转到 treeviews 属性面板并单击面板中看起来像闪电的图标来完成,然后滚动直到看到文本DrawNode,双击它。

Now you just copy and paste this into the created method

现在您只需将其复制并粘贴到创建的方法中

if (e.Node.ImageIndex >= e.Node.TreeView.ImageList.Images.Count) // if there is no image
{
    int imagewidths = e.Node.TreeView.ImageList.ImageSize.Width;
    int textheight = TextRenderer.MeasureText(e.Node.Text, e.Node.NodeFont).Height;
    int x = e.Node.Bounds.Left - 3 - imagewidths / 2;
    int y = (e.Bounds.Top + e.Bounds.Bottom) / 2+1;

    Point point = new Point(x - imagewidths/2, y - textheight/2); // the new location for the text to be drawn

    TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.NodeFont, point, e.Node.ForeColor);
}
else // drawn at the default location
    TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.TreeView.Font, e.Bounds, e.Node.ForeColor);

回答by MtnManChris

What I have chosen to do is to use an image of dots for those TreeViewnodes that are not supposed to have an image.

我选择做的是为那些TreeView不应该有图像的节点使用点图像。

TreeNode with image dots

带有图像点的 TreeNode

I add this image as the last image in the list, and if the item is not supposed to have an image I set it to ImageList.Images.Count-1

我将此图像添加为列表中的最后一个图像,如果该项目不应该有图像,我将其设置为 ImageList.Images.Count-1

回答by Rogerio Souza

Hei bro, i found a way. Set the first image as an empty image, like this...

嘿兄弟,我找到了方法。将第一张图像设置为空图像,就像这样......

TreeView treeView = new TreeView();
treeView.ImageList.Images.Add(new Bitmap(1,1));

So, the index 0 is an empty image. I hope this helps

因此,索引 0 是一个空图像。我希望这有帮助

回答by Aussie Ash

I found that using StateImageList on the TreeView instead of ImageList will only show the image when StateImageIndex on the TreeNode is equal or greater than 0

我发现在 TreeView 上使用 StateImageList 而不是 ImageList 只会在 TreeNode 上的 StateImageIndex 等于或大于 0 时显示图像