WPF:通过解析字符串的树视图 [英] WPF: Tree view by parsing string

查看:24
本文介绍了WPF:通过解析字符串的树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的树最多只能显示 2 个级别?例如,NetworkControl.AddressData.MessageOriginatorID.Value 仅将 NetworkControl 显示为 root,将 AddressData 显示为子项,但 AddressData不会将 MessageOriginatorID.Value 显示为子项.

Why is my tree displaying only up to 2 levels? For example, NetworkControl.AddressData.MessageOriginatorID.Value shows only NetworkControl as root and AddressData as a child but AddressData doesn't show MessageOriginatorID.Value as a child.

输入数据:

       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();


    }       

将数据填充到树状视图的方法:

Method that populates the data to 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>

推荐答案

你需要递归地将 ChildrenTreeModel 添加到 TreeViewModel>.

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

您可以为此创建一个辅助函数或扩展方法,或者直接将逻辑添加到您的GetRequestTreeNodesFromModel"

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

这是一个从 TreeModel 集合中递归创建 TreeViewModel 集合的示例

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() };
    }
}

然后你可以在你的 GetRequestTreeNodesFromModel 函数中使用它

Then you can use that in your GetRequestTreeNodesFromModel function

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

结果:

这篇关于WPF:通过解析字符串的树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆