WPF:解析字符串的树视图

taor4pac  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(195)

为什么我的树最多只能显示两个级别?例如,NetworkControl.AddressData.MessageOriginatorID.Value只显示NetworkControl作为根,AddressData作为子,但AddressData不显示MessageOriginatorID.Value作为子。
输入数据:

public List<TreeModel> GetRequestTreeNodes()
        {

        string[] nodes = {"NetworkControl.AlternateIndexText.Value",
                             "NetworkControl.AddressData.DestinationID",
                             "NetworkControl.AddressData.MessageOriginatorID.Value",
                             "NetworkControl.AddressData.TransactionOriginatorID.Value",
                             "NetworkControl.AddressData.MessageOriginatorID.Value",
                             "VehicleIdentification.IdentificationID.Value",
                             "VehicleSummary.VehicleIdentification.IdentificationID.Value",
                             "TitleSummary.TitleIdentification.IdentificationID.Value",
                             "TitleSummary.JurisdictionTitlingKeyText.Value",
                             "VehicleSummary.VehicleIdentification.IdentificationID.Value",
                              };

        List<TreeModel> nodeList = BuildTree(nodes);

        return nodeList;

    }

解析器方法:

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
          {
              Name = g.Key,
              Children = BuildTree(
                from s in g
                where s.Length > g.Key.Length + 1
                select s.Substring(g.Key.Length + 1))
          }
          ).ToList();

    }

将数据填充到treeview的方法:

public List<TreeViewModel> GetRequestTreeNodesFromModel()
       {
        treeNodeViewModel = treeModel.GetRequestTreeNodes().Select(a => new TreeViewModel
        {
            Children = a.Children.Select(c => new TreeViewModel { Name = c.Name }).ToList(),
            Name = a.Name,

        }).ToList();

        return treeNodeViewModel;
    }

XAML

<TreeView Margin="644,137,6,6" Grid.RowSpan="2" ItemsSource="{Binding Path=TreeView}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:TreeViewModel}" ItemsSource="{Binding Path= Children}">
                        <CheckBox IsChecked="{Binding Name}" Content="{Binding Name}" />
                    <!--<TextBlock Text="{Binding Name}" />-->
                </HierarchicalDataTemplate>
                </TreeView.Resources>
        </TreeView>
dojqjjoe

dojqjjoe1#

您需要递归地将ChildrenTreeModel添加到TreeViewModel
您可以为此创建一个辅助函数或扩展方法,或者直接将逻辑添加到'GetRequestTreeNodesFromModel'中
下面是一个从TreeModel集合递归创建TreeViewModel集合的示例

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

然后,您可以在GetRequestTreeNodesFromModel函数中使用它

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

结果:

相关问题