我有一个Observable路径集合。 我想要做的是更新我的treeView更改集合。 你能否帮我创建一个方法,将Treeview,FilePath和PathSeparator作为参数并将新节点添加到我的treeView中。这就是我现在所拥有的:
private void MyCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
TreeViewAddNode(TreeView,Path,PathSeparator)
}
TreeViewAddNode(TreeView treeView, string path, char pathSeparator)
{
foreach (string subPath in path.Split(pathSeparator))
{
//Hear should be logic to add new nodes if they don't exist }
}
}
结果我想有类似的东西:
C:
--Temp
---- FILE1.TXT
---- FILE2.TXT
----新的Foledr
------- File3.txt
--AnotherFolder
---- File4.txt
d:
- 新文件夹
---- File.txt
答案 0 :(得分:1)
修改强>
现在可以更好地了解所询问的内容:
private void TreeViewAddNode(TreeView treeView, string path, char pathSeparator)
{
string[] split = path.Split(pathSeparator);
for(int i = 0; i < split.Length; i++)
{
if(i == 0)
{
checkTreeView(treeView, split[0]);
}
else
{
TreeNode node = treeView1.Nodes.Find(split[i - 1], true)[0];
checkNodes(node, split[i]);
}
}
}
private void checkTreeView(TreeView treeView, string path)
{
bool exists = false;
foreach(TreeNode node in treeView.Nodes)
{
if(node.Text == path)
{
exists = true;
}
}
if(!exists)
{
TreeNode node = treeView.Nodes.Add(path);
node.Name = path;
}
}
private void checkNodes(TreeNode parent, string path)
{
bool exists = false;
foreach(TreeNode node in parent.Nodes)
{
if(node.Text == path)
{
exists = true;
}
}
if(!exists)
{
TreeNode node = parent.Nodes.Add(path);
node.Name = path;
}
}
checkTreeView检查树视图节点中是否已存在路径。如果它没有将它添加到树视图中。 checkNodes也是如此。