我想通过以下方法调用根和子来构建动态树。我有一个 TransactionTypeViewModel,它有 3 个方法
tranViewModel.GetWSMethodName()
tranViewModel.GetAllRequest()
tranViewModel.GetAllResponse()
我有这个字符串列表,我需要循环遍历方法并将项目添加到下面的字符串列表中:字符串将用于显示树的根和子。
List<String> wsMethodNameList = new List<String>(); // root of tree from tranViewModel.GetWSMethodName()
List<String> requestXmlList = new List<String>(); // child1 tranViewModel.GetAllRequest()
List<String> responseXmlList = new List<String>(); // child2 tranViewModel.GetAllResponse()
一旦我从我想要构建我的树的每个方法中获得字符串列表。
List<TreeViewModel> nodes = getAllTreeNodesFromModel();
var request = new TreeViewModel();
var response = new TreeViewModel();
var parent = new TreeViewModel();
List<TreeViewModel> requestNodes = new List<TreeViewModel>();
for (int i = 0; i < requestXmlList.Count; i++)
{
// requestNodes = new List<TreeViewModel>();
request = new TreeViewModel { Value = requestXmlList[i] };
//requestNodes.Add(request);
request.Children = nodes;
}
List<TreeViewModel> responseNodes = new List<TreeViewModel>();
for (int i = 0; i < responseXmlList.Count; i++)
{
// responseNodes = new List<TreeViewModel>();
response = new TreeViewModel { Value = responseXmlList[i] };
// responseNodes.Add(response);
response.Children = nodes;
}
List<TreeViewModel> parentNode = new List<TreeViewModel>();
for (int i = 0; i < wsMethodNameList.Count; i++)
{
// parentNode = new List<TreeViewModel>();
parent = new TreeViewModel { Value = wsMethodNameList[i] };
// parentNode.Add(parent);
parent.Children.Add(request);
parent.Children.Add(response);
}
return new ObservableCollection<TreeViewModel> { parent };
在我执行完这一切之后,我得到的是重复的树视图。我找不到更简单的方法来解决这个问题。有人可以帮我理解吗?