0

我使用了带有ImageSet="XPFileExplorer"的 Treeview 我创建了一个循环来将一组目录填充到 TreeNodes 中,填充到作为 Treeviews 节点的 treenodecollection 中。这有效并给了我一个代表目录层次结构的树视图。但是,图标都是错误的。有些是计算机图标,有些是文本文件图标,有些是文件夹图标。我的问题是如何让它们都成为文件夹图标。我看到有一种方法可以在 TreeNodes 上设置 ImageUrl,但是我不想制作自己的图像。我更喜欢使用ImageSet="XPFileExplorer"提供的图像 这是我用来制作节点的 asp 和方法。

<asp:TreeView ID="TreeViewfilelist" runat="server" 
            onload="TreeViewfilelist_Load" ImageSet="XPFileExplorer" 
        NodeIndent="15">
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
            <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" 
                HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" 
                HorizontalPadding="0px" VerticalPadding="0px" />
</asp:TreeView>

调用加载

TreeNodeCollection main = TreeViewfilelist.Nodes;
makemainNodes(main, dir);

方法:

protected void makemainNodes(TreeNodeCollection treec, System.IO.DirectoryInfo directory)
    {
        foreach (System.IO.DirectoryInfo g in directory.GetDirectories())
        {               
            TreeNode child = new TreeNode(g.Name);                
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(g.FullName);
            makesubNodes(child, dir);
            treec.Add(child);
        } 
    }
protected void makesubNodes(TreeNode treec, System.IO.DirectoryInfo directory)
    {
        foreach (System.IO.DirectoryInfo g in directory.GetDirectories())
        {              
            TreeNode child = new TreeNode(g.Name);
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(g.FullName);
            makesubNodes(child, dir);
            treec.ChildNodes.Add(child);
        } 
    }
4

1 回答 1

1

我找不到使用内置 ImageSet="XPFileExplorer" 将所有图标更改为文件夹图标的任何方法。相反,我创建了自己的 folder.png 图像,添加到项目中的 Images 文件夹中,并为我使用的树创建节点

child.ImageUrl = "~/Images/folder.png";

在上面的例子中,

protected void makemainNodes(TreeNodeCollection treec, System.IO.DirectoryInfo directory)
{
    foreach (System.IO.DirectoryInfo g in directory.GetDirectories())
    {               
        TreeNode child = new TreeNode(g.Name);                
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(g.FullName);
        child.ImageUrl = "~/Images/folder.png";            
        makesubNodes(child, dir);
        treec.Add(child);
    } 
}

protected void makesubNodes(TreeNode treec, System.IO.DirectoryInfo directory)
{
    foreach (System.IO.DirectoryInfo g in directory.GetDirectories())
    {              
        TreeNode child = new TreeNode(g.Name);
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(g.FullName);
        child.ImageUrl = "~/Images/folder.png";
        makesubNodes(child, dir);
        treec.ChildNodes.Add(child);
    } 
}
于 2013-11-12T19:38:47.750 回答