我正在开发 WPF 项目以在树视图中显示路径列表。我有 propertyPaths(例如:NetworkControl.AlternateIndexText.Value")路径有 id。
构造函数:
public MessageElement()
{
Children = new List<MessageElement>();
messageElements = new List<MessageElement>();
}
public MessageElement(int id, string name, List<MessageElement> children)
{
this.ID = id;
this.Name = name;
this.Children = children;
}
public MessageElement(int id, string path)
{
this.ID = id;
this.path = path;
}
在这里,我展示了我是如何进行解析的。
我正在从传递给 BuildTree 方法的 IEnumerable propertyPath 构建孩子。
public List<MessageElement> BuildTree(IEnumerable<string> propertyPath)
{
return (
from path in propertyPath // Ex: NetworkControl.AlternateIndexText.Value"
let splitPath = path.Split('.')
group path by path.Split('.')[0] into pathElementGroup
select new MessageElement(ID, path)
{
// ID = Id,
Name = pathElementGroup.Key, //name of each tree node
Children = BuildTree( //create child from the propertyPath
from propertyPathElement in pathElementGroup
where propertyPathElement.Length > pathElementGroup.Key.Length + 1
select propertyPathElement.Substring(pathElementGroup.Key.Length + 1))
}).ToList();
}
填充数据:
public List<MessageElement> GetRequestTreeNodes()
{
messageElements.Add(new MessageElement(1, "NetworkControl.AlternateIndexText.Value"));
messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.DestinationID"));
messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.MessageOriginatorID.Value"));
}
在这里,我正在准备一个列表,我将从 messageElements 列表传递给 BuildTree 方法。
String[] propertyPath = messageElements.Select(I => Convert.ToString(I.path)).ToArray();
// int[] idList = messageElements.Select(I => (I.id)).ToArray();
List<MessageElement> nodeList = BuildTree(propertyPath);
return nodeList;
如何在用于构建树节点的 propertyPath 列表和 Id 之间创建关联。例如 MessageElement(1, "NetworkControl.AlternateIndexText.Value"));