非常简单的概念,但我在这里引人注目。 我正在尝试构建一个像Path这样的简单文件浏览器,但我似乎无法直截了当。
构建Path的方法是:
private string BuildFullPath(List<Project> Children)
{
string path = string.Empty;
foreach(Project project in Children)
{
if (this.ParentFolder == null)
{
path = this.Name;
}
else
{
path += this.ParentFolder.Name + " > " + this.Name;
}
}
return path;
}
假数据是
Projects = new ObservableCollection<Project>();
Project parentOne = new Project("Apple", true, null);
Project parentTwo = new Project("Samsung", true, null);
Project parentThree = new Project("Google", true, null);
Project parentFour = new Project("Amazon", true, null);
Project parentOneChildOne;
parentOneChildOne = new Project("Mac", true, parentOne);
Project parentOneChildTwo;
parentOneChildTwo = new Project("iPhone", true, parentOne);
Project parentOneChildThree;
parentOneChildThree = new Project("iPad", true, parentOne);
parentOne.Children.Add(parentOneChildOne);
parentOneChildOne.Children.Add(new Project("MacBook", true, parentOneChildOne));
parentOneChildOne.Children.Add(new Project("MacBook Pro", true, parentOneChildOne));
parentOneChildOne.Children.Add(new Project("MacBook Air", true, parentOneChildOne));
projects.Add(parentOne);
所以MacBook Pro的路径应该是Apple - &gt; Mac-&gt; MacBook - &gt; MacBook Pro和Mac的路径应该只是Apple - &gt; Mac但似乎无法动摇它。
答案 0 :(得分:2)
沿着这条路线......
private string BuildFullPath(Project project)
{
string path = string.Empty;
while(project != null) {
if(path != string.Empty)
path = "->" + path;
path = project.Name + path
project = project.ParentFolder;
}
return path;
}