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
How do I set an image for some but not all nodes in a TreeView?
提问by Simon
I have a TreeView
windows forms control with an ImageList
, and I want some of the nodes to display images, but the others to not have images.
我有一个带有 的TreeView
windows 窗体控件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 ImageKey
and ImageIndex
to "not set" values the control just defaults ImageIndex
to 0. The following code:
如果您尝试同时设置ImageKey
和ImageIndex
“未设置”值,则控件默认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 ImageIndex
and SelectedImageIndex
to 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 TreeNode
inserted into your TreeView
. I changed the background color of my TreeView
and 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 TreeNode
text where the image should have been, getting rid of the white space.
这将TreeNode
在图像应该出现的地方绘制文本,消除空白。
You'll need to set the TreeView
's DrawMode
property to OwnerDrawText
. You can find the DrawMode
property in the properties panel.
您需要将TreeView
的DrawMode
属性设置为OwnerDrawText
。您可以DrawMode
在属性面板中找到该属性。
Next when you add a node, set it's ImageIndex
and SelectedImageIndex
greater than the value of your yourImageListName.Images.Count
value. This is so no image will be drawn, but there will still be that white space you don't want.
接下来,当你添加一个节点,设置它的ImageIndex
和SelectedImageIndex
比你的价值更大的yourImageListName.Images.Count
价值。这样就不会绘制图像,但仍然会有您不想要的空白区域。
Now to get rid the white space. Add a handle for the treeviews DrawNode
event. 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 TreeView
nodes that are not supposed to have an image.
我选择做的是为那些TreeView
不应该有图像的节点使用点图像。
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 时显示图像