我正在WPF項目上顯示TreeView
中的路徑列表。我有propertyPaths(例如:NetworkControl.AlternateIndexText.Value「)路徑已經IDTreeView:如何保留propertypath的Id?
TreeModel的
public class MessageElement
{
private int id;
public string Name { get; set; }
public string path { get; set; }
public List<MessageElement> Children { get; set; }
public List<MessageElement> messageElements { get; set; }
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;
}
}
MOQ一些數據
public List<MessageElement> GetRequestTreeNodes()
{
messageElements.Add(new MessageElement(1, "NetworkControl.AlternateIndexText.Value"));
messageElements.Add(new MessageElement(2, "NetworkControl.AddressData.DestinationID"));
messageElements.Add(new MessageElement(3, "NetworkControl.AddressData.MessageOriginatorID.Value"));
messageElements.Add(new MessageElement(4, "VehicleSummary.VehicleIdentification.IdentificationID.Value"));
messageElements.Add(new MessageElement(4, "TitleSummary.JurisdictionTitlingKeyText.Value"));
messageElements.Add(new MessageElement(6, "VehicleSummary.VehicleIdentification.IdentificationID.Value"));
return messageElements;
}
遞歸創建樹:。
public List<MessageElement> BuildTree(IEnumerable<MessageElement> messageElements)
{
return (
from element in messageElements // Ex:(1, "NetworkControl.AlternateIndexText.Value")
let elementId = element.id // get id from message element
let splitPath = element.path.Split('.') // get path from message element
group element by element.path.Split('.')[0] into pathElementGroup
select new MessageElement(ID, path)
{
ID = elementId, ?? // id of each path Ex: 1 => "NetworkControl.AlternateIndexText.Value"
// this is the hardest part I have to reserve each ID that belongs to the path
//Like I showed in the example
Name = pathElementGroup.Key, //name of each tree node to be displayed on tree
Children = BuildTree( //create child from the propertyPath
(from propertyPathElement in pathElementGroup
where propertyPathElement.path.Length > pathElementGroup.Key.Length + 1
select new MessageElement())
.ToList<MessageElement>())
}
);
}
}
如何爲每個屬性路徑保留每個ID,用於構建我的樹節點。
我調試你的代碼,它表明id被分配給只有最後一個(葉)元素有ID。這就是你所評論的。我想要做的是如何創建一個(Key,Value)對,併爲每個路徑指定指定的ID。例如,如果我想在ID = 1的路徑和ID = 1的網格元素之間創建關係。 –
這是我嘗試過的:http://stackoverflow.com/questions/16993507/wpf-treeview-from-keyvaluepairint-string?noredirect=1#comment24556019_16993507 –