WPF: Tree view by parsing string

后端 未结 1 1603
无人共我
无人共我 2021-01-16 22:05

Why is my tree displaying only up to 2 levels? For example, NetworkControl.AddressData.MessageOriginatorID.Value shows only NetworkControl as root

相关标签:
1条回答
  • 2021-01-16 22:38

    You need to recursively add the Children from the TreeModel to the TreeViewModel.

    You could make a helper function or extension method for this or just add the logic directly to your 'GetRequestTreeNodesFromModel'

    Here is a example that recursively creates your TreeViewModel collection from the TreeModel collection

    public IEnumerable<TreeViewModel> ToTreeViewModel(IEnumerable<TreeModel> treemodel)
    {
        foreach (var item in treemodel)
        {
            yield return new TreeViewModel { Name = item.Name, Children = ToTreeViewModel(item.Children).ToList() };
        }
    }
    

    Then you can use that in your GetRequestTreeNodesFromModel function

    public List<TreeViewModel> GetRequestTreeNodesFromModel()
    {
        return  ToTreeViewModel(treeModel.GetRequestTreeNodes()).ToList();
    }
    

    Result:

    enter image description here

    0 讨论(0)
提交回复
热议问题