我有一个由分隔符分隔的路径数组:
Dim paths = New List(Of String)() From {
"C:\WINDOWS\AppPatch\MUI\040C",
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
"C:\WINDOWS\Microsoft.NET\Framework\addins\MUI",
"C:\WINDOWS\addins",
"C:\WINDOWS\AppPatch",
"C:\WINDOWS\AppPatch\MUI",
"C:\WINDOWS\Microsoft.NET\Framework\MUI\MUI\0409"
}
我想创建一个看起来像这样的RadTreeView:
+C:
+Windows
+AppPatch
+addins
+Microsoft.NET
+Framework
...
这是我到目前为止所做的事情,但有一些我不知道的事情:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim paths = New List(Of String)() From {
"C:\WINDOWS\AppPatch\MUI\040C",
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
"C:\WINDOWS\addins",
"C:\WINDOWS\AppPatch",
"C:\WINDOWS\AppPatch\MUI",
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
}
If Not Page.IsPostBack Then
Dim subPathAgg As String
Dim pathSeparator As String = "\"
' For each complete individual path
For Each path As String In paths
subPathAgg = String.Empty
' Fill array of strings with each delimited part
Dim arrFolders As List(Of String) = (path).Split(pathSeparator).ToList
Dim lastNode As RadTreeNode = Nothing
Dim iCount As Integer = 0
'For each one of the folders
For Each folder As String In path.Split(pathSeparator)
subPathAgg += folder & pathSeparator
Dim foundNode As RadTreeNode = RadTreeView1.Nodes.FindNodeByValue(subPathAgg, True)
If foundNode Is Nothing Then
If lastNode Is Nothing Then
lastNode = New RadTreeNode(folder, subPathAgg)
RadTreeView1.Nodes.Add(lastNode)
Else
Dim otherNode = New RadTreeNode(folder, subPathAgg)
lastNode.Nodes.Add(otherNode)
lastNode = otherNode
End If
Else
If foundNode.Text <> folder Then
Dim otherNode = New RadTreeNode(folder, subPathAgg)
foundNode.Nodes.Add(otherNode)
lastNode = otherNode
End If
End If
Next
lastNode = Nothing
Next
End If
End Sub
答案 0 :(得分:0)
我设法在Telerik论坛上找到解决方案:
C#
private void AddNode(RadTreeView treeView, string path)
{
List<string> folders = GetPaths(path);
folders.Reverse();
for (int i = 0; i < folders.Count; i++)
{
RadTreeNode node = treeView.FindNodeByValue(folders[i]);
string folderName = GetCurrentFolder(folders[i]);
if (node == null)
{
if (i == 0)
{
treeView.Nodes.Add(new RadTreeNode(folderName, folders[i]));
}
else
{
treeView.FindNodeByValue(folders[i - 1]).Nodes.Add(new RadTreeNode(folderName, folders[i]));
}
}
else
{
if (node.Text != folderName)
{
node.Nodes.Add(new RadTreeNode(folderName, folders[i]));
}
}
}
}
private string GetCurrentFolder(string path)
{
var folders = path.Split(new string[] { "\\" }, StringSplitOptions.None);
return folders[folders.Count() - 1];
}
private List<string> GetPaths(string path)
{
List<string> paths = new List<string>();
paths.Add(path);
while (path.LastIndexOf("\\") >= 0)
{
path = path.Substring(0, path.LastIndexOf("\\"));
paths.Add(path);
};
return paths;
}
protected void Page_Load(object sender, System.EventArgs e)
{
dynamic paths = new List<string> {
"C:\\WINDOWS\\AppPatch\\MUI\\040C",
"C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727",
"C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\MUI",
"C:\\WINDOWS\\addins",
"C:\\WINDOWS\\AppPatch",
"C:\\WINDOWS\\AppPatch\\MUI",
"C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\MUI\\0409"
};
if (!Page.IsPostBack)
{
foreach (string path in paths)
{
AddNode(RadTreeView1, path);
}
}
}
VB.NET:
Private Sub AddNode(treeView As RadTreeView, path As String)
Dim folders As List(Of String) = GetPaths(path)
folders.Reverse()
For i As Integer = 0 To folders.Count - 1
Dim node As RadTreeNode = treeView.FindNodeByValue(folders(i))
Dim folderName As String = GetCurrentFolder(folders(i))
If node Is Nothing Then
If i = 0 Then
treeView.Nodes.Add(New RadTreeNode(folderName, folders(i)))
Else
treeView.FindNodeByValue(folders(i - 1)).Nodes.Add(New RadTreeNode(folderName, folders(i)))
End If
Else
If node.Text <> folderName Then
node.Nodes.Add(New RadTreeNode(folderName, folders(i)))
End If
End If
Next
End Sub
Private Function GetCurrentFolder(path As String) As String
Dim folders = path.Split(New String() {"\"}, StringSplitOptions.None)
Return folders(folders.Count() - 1)
End Function
Private Function GetPaths(path As String) As List(Of String)
Dim paths As New List(Of String)()
paths.Add(path)
While path.LastIndexOf("\") >= 0
path = path.Substring(0, path.LastIndexOf("\"))
paths.Add(path)
End While
Return paths
End Function
Protected Sub Page_Load(sender As Object, e As System.EventArgs)
Dim paths As dynamic = New List(Of String)() From { _
"C:\WINDOWS\AppPatch\MUI\040C", _
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727", _
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI", _
"C:\WINDOWS\addins", _
"C:\WINDOWS\AppPatch", _
"C:\WINDOWS\AppPatch\MUI", _
"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409" _
}
If Not Page.IsPostBack Then
For Each path As String In paths
AddNode(RadTreeView1, path)
Next
End If
End Sub