我正在开发一个wpf项目,我必须递归地构建树节点。我想要做的是为另一个模型的节点分配标头,例如:
Request Header --> node1
-->node1.1
-->node1.2
Response Header -->node2
-->node2.1
-->node2.2
// building tree
public List<TreeModel> BuildTree(IEnumerable<string> strings)
{
return (
from s in strings
let split = s.Split('.')
group s by s.Split('.')[0] into g
select new TreeModel
{
Value = g.Key,
Children = BuildTree(
from s in g
where s.Length > g.Key.Length + 1
select s.Substring(g.Key.Length + 1))
}
).ToList();
}
// Here I am creating static MessageElementModel objects
我创建这些对象的原因是我想将它们与另一个控件(组合框)相关联以在select事件上创建,但这不是我的问题的一部分。
public List<MessageElementModel> GetAllMessageElements()
{
string[] request = { "NetworkControl.AlternateIndexText.Value" };
string[] request2 = { "NetworkControl.NetworkControl.AddressData.TransactionOriginatorID.Value" };
string[] request3 = { "VehicleSummary.VehicleIdentification.IdentificationID.Value" };
transactionTypeWSRequestXmlType = TransactionTypeModle.TransactionTypeWSRequestXmlType;
transactionTypeWSResponseXmlType = TransactionTypeModel.TransactionTypeWSRequestXmlType;
messageElements.Add(new MessageElementModel(1, transactionTypeWSRequestXmlType,transactionTypeWSResponseXmlType, TreeNode.BuildTree(request)));
messageElements.Add(new MessageElementModel(1, transactionTypeWSRequestXmlType,transactionTypeWSResponseXmlType, TreeNode.BuildTree(request2)));
return messageElements;
}
The transactionTypeWSRequestXmlType,transactionTypeWSResponseXmlType and transactionTypeWSRequestXmlType,transactionTypeWSResponseXmlType are the headers of the tree (TreeNode.BuildTree(request))
在XAML中我有
<HierarchicalDataTemplate DataType="{x:Type local:TreeViewModel}" ItemsSource="{Binding Children}">
<CheckBox IsChecked="{Binding Value}" Content="{Binding Value}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
How can I add header from another model? for this case transactionTypeWSRequestXmlType,transactionTypeWSResponseXmlType and transactionTypeWSRequestXmlType,transactionTypeWSResponseXmlType to the treeview for the nodes I am creating in the TreeNode.BuildTree(request) recursively?
and the same for var response;
var treeNode = new ObservableCollection<TransactionTypeViewModel>() { request, response };
}
return treeNode;
}
In the messageElementViewModel I am getting the constructed nods(TreeNodeModel)+roots(TransactionTypeViewModel)
here I addded Id so that when I chose any transaction from my Transaction combobox the tree will display only matching Ids
public List<MessageElementViewModel> GetAllTreeNodesList()
{
messageElements = TransactionType.getAllTreeNodes().Select(msgElements => new MessageElementViewModel
{
Id = msgElements.Id,
Request = msgElements.Request,
Response = msgElements.Response,
}).ToList();
return messageElements;
}
//make it observable
public ObservableCollection<MessageElementViewModel> GetAllTreeNodes()
{
observableMessageElement = new ObservableCollection<MessageElementViewModel> (messageElements);
return observableMessageElement;
}
//xaml binding to MessageElementViewModel where I have the roots and nodes combination
How can I bind all these together in the xaml?