1

我想通过以下方法调用根和子来构建动态树。我有一个 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 };

在我执行完这一切之后,我得到的是重复的树视图。我找不到更简单的方法来解决这个问题。有人可以帮我理解吗?

4

1 回答 1

0

我想我发现了问题所在。我正在为我拥有的每个列表创建循环。这是我如何清理它的:

       for (int i = 0; i < wsMethodNameList.Count; i++)
        {
            var child = new TreeViewModel { Value = wsMethodNameList[i] };
            parent.Children.Add(child);

            var requestChild = new TreeViewModel { Value = requestXmlList[i] };

            requestChild.Children = nodes;
            child.Children.Add(requestChild);


            var responseChild = new TreeViewModel { Value = responseXmlList[i] };

            responseChild.Children = nodes;
            child.Children.Add(responseChild);               

        }

        return new ObservableCollection<TreeViewModel> { parent };
于 2013-06-06T12:59:55.460 回答