WPF树形菜单--递归与非递归遍历生成树结构的集合

一、新建了WPF项目作为测试,使用TreeView控件进行界面展示。

第一步创建实体类TreeEntity:

public class TreeEntity
{
private int _mid;
private string _title;
private int _pid;
private List<TreeEntity> _childrens=new List<TreeEntity>();
/// <summary>
/// 项ID
/// </summary>
public int Mid
{
get { return _mid; }
set
{
_mid = value;
}
}
/// <summary>
/// 项名称
/// </summary>
public string Title
{
get { return _title; }
set
{
_title = value;
}
}
/// <summary>
/// 父亲ID
/// </summary>
public int Pid
{
get { return _pid; }
set
{
_pid = value;
}
} /// <summary>
/// 儿子们
/// </summary>
public List<TreeEntity> Childrens
{
get { return _childrens; }
set
{
_childrens = value;
}
}
}

第二部构造无层级的数据源:

TreeEntity yeye = new TreeEntity()
{
Mid = ,
Title = "猪爷爷",
Pid =
};
TreeEntity laolao = new TreeEntity()
{
Mid = ,
Title = "猪姥姥",
Pid =
};
TreeEntity baba = new TreeEntity()
{
Mid = ,
Title = "猪爸爸",
Pid =
};
TreeEntity mama = new TreeEntity()
{
Mid = ,
Title = "猪妈妈",
Pid =
};
TreeEntity peiqi = new TreeEntity()
{
Mid = ,
Title = "佩奇",
Pid =
};
TreeEntity peizierzi = new TreeEntity()
{
Mid = ,
Title = "小佩佩奇",
Pid =
};
TreeEntity peiqisunzi = new TreeEntity()
{
Mid = ,
Title = "佩奇的孙子",
Pid =
};
TreeEntity peiqinver = new TreeEntity()
{
Mid = ,
Title = "小佩佩妞",
Pid =
};
TreeEntity qiaozhi = new TreeEntity()
{
Mid = ,
Title = "乔治",
Pid =
};
List<TreeEntity> treeEntities = new List<TreeEntity>()
{
yeye,
laolao,
mama,
peiqi,
peiqinver,
peiqisunzi,
qiaozhi,
peizierzi
};

第三步实现树形集合的构造

方法一:使用递归方法构造

1)、先取出*数据项--即无父亲的项

List<TreeEntity> rootLists = treeEntities.Where(x => x.Pid == ).ToList();
for (int i = ; i < rootLists.Count; i++)
{
rootLists[i].Childrens = CreateTree(rootLists[i].Mid, treeEntities);
}

2)、再遍历上一步取得的数据项,使用递归获取所有子项

private List<TreeEntity> CreateTree(int pid, List<TreeEntity> sources)
{
List<TreeEntity> childrens = new List<TreeEntity>();
if (sources != null && sources.Count > )
{
childrens = sources.Where(x => x.Pid == pid).ToList();
if (childrens != null & childrens.Count > )
{
foreach (var item in childrens)
{
item.Childrens = CreateTree(item.Mid, sources);
}
}
}
return childrens;
}

3)、将集合绑定给TreeView的数据源

MyTreeList.ItemsSource = rootLists;

方法二:非递归遍历

1)、将所有数据项装进字典中 字典中的键为 当前实例的ID;字典中的项为当前实例

Dictionary<int, TreeEntity> valuePairs = new Dictionary<int, TreeEntity>();
foreach (var item in treeEntities)
{
valuePairs[item.Mid] = item;
}
List<TreeEntity> rootLists =CreateTreeQuick(valuePairs);

2)、遍历字典中的所有Values,向当前项(item)的PID对应的实例中添加当前项(item)

private List<TreeEntity> CreateTreeQuick(Dictionary<int, TreeEntity> keyValues)
{
List<TreeEntity> myTree = new List<TreeEntity>();
foreach (var item in keyValues.Values)
{
if (item.Pid==)
{
myTree.Add(item);
}
else
{
if (keyValues.ContainsKey(item.Pid))
{
keyValues[item.Pid].Childrens.Add(item);
}
}
}
return myTree;
}

3)、将集合绑定到TreeView控件的数据源中

MyTreeList.ItemsSource = rootLists;

二、WPF前台布局

<TreeView x:Name="MyTreeList" Width="200">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TreeEntity}" ItemsSource="{Binding Childrens}">
<TextBlock Text="{Binding Title}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
上一篇:vue.js 树菜单 递归组件树来实现


下一篇:git支持中文