简体   繁体   English

TreeView不显示ImageList中的图像

[英]TreeView Not Displaying Images from ImageList

I have a TreeView that displays CheckBox es: 我有一个显示CheckBox es的TreeView

在此输入图像描述

I want to check if a given directory contains an ".mdf" database and if it does, check whether it is attached on the selected server instance. 我想检查给定目录是否包含“.mdf”数据库,如果是,请检查它是否附加在所选服务器实例上。 If the database is attached I display an image against that node, and a different image if it is not attached. 如果附加了数据库,则会针对该节点显示图像,如果未附加,则显示不同的图像。 Note: The images are .png format, size 32x32... 注意:图像为.png格式,大小为32x32 ...

I populate an ImageList from Properties.Resources 我从Properties.Resources填充ImageList

mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);

I then loop through the tree and add the relevant image 然后我循环遍历树并添加相关图像

public static void RecursiveAddImage(TreeNode treeNode, List<string> attachedList)
{
    if (String.Compare(Path.GetExtension(treeNode.Text), ".mdf", true) == 0)
    {
        string databaseName = treeNode.Text.Replace(".mdf", String.Empty);
        if (attachedList.Contains(databaseName))
        {
            treeNode.ImageIndex = 0;
            treeNode.SelectedImageIndex = 0;
        }
        else
        {
            treeNode.ImageIndex = 1;
            treeNode.SelectedImageIndex = 1;
        }
    }
    foreach (TreeNode node in treeNode.Nodes)
        RecursiveAddImage(node, attachedList);
}

The above code goes through the loop with no complaints, finds ".mdf"s and seems to add the relevant ImageIndexes but these do not show up in the TreeView . 上面的代码通过循环没有任何抱怨,找到“.mdf”,似乎添加了相关的ImageIndexes但这些没有出现在TreeView What am I doing wrong here and can I add the ImageList at design time (something I also can't seem to do)? 我在这里做错了什么,我可以在设计时添加ImageList (我似乎也做不到的事情)?

I have read several posts and ofcourse the MSDN documantation but I still can't seem to get it working. 我已经阅读了几篇文章和MSDN文档,但我仍然无法让它工作。 Any help as always, is much appreciated. 一如既往的任何帮助,非常感谢。

Make sure the TreeView control has the ImageList property set to the correct ImageList reference: 确保TreeView控件将ImageList属性设置为正确的ImageList引用:

mainImageList = new ImageList();
mainImageList.Images.Add(Properties.Resources.Database);
mainImageList.Images.Add(Properties.Resources.DatabaseGrey);

treeView1.ImageList = mainImageList;

TreeNode.StateImageIndex= 0; would set the imagelist images. 将设置图像列表图像。 Make sure the imagelist is binded to Treeview control as mentioned above. 如上所述,确保将图像列表绑定到Treeview控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM