我使用了Treeview与 ImageSet =“XPFileExplorer”我已经创建了一个循环来将一组目录填充到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>
致电onload
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);
}
}
答案 0 :(得分: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);
}
}