如何从WPF中的对象列表创建TreeView

时间:2016-11-11 12:48:22

标签: wpf

我有一个引用ID,Name和ParentID的对象列表。我试图创建树视图。但它没有显示任何东西。你能帮我创建一个treeView.I我正在共享下面的代码。

public class WorkFlowScriptIDDataStore
{
    public string Id;        
    public string Name;
    public string ParentId;

    public List<WorkFlowScriptIDDataStore> Subcategories;
    public void Clear()
    {
        Id = "";
        Name = "";
        ParentId = "";
    }
}
private void PopulateTreeView()
{
        List<WorkFlowScriptIDDataStore> localstore = new         List<WorkFlowScriptIDDataStore>();
        List<WorkFlowScriptIDDataStore> WorkFlowScriptIDDataList = new List<WorkFlowScriptIDDataStore>();
        WorkFlowScriptIDDataList.Clear();

       int i = 0;
        foreach (string node in CurrentScriptids)
        {
            WorkFlowScriptIDDataList.Add(new WorkFlowScriptIDDataStore());
            if (node.Contains(StatusCodes.TAC_TXT_SCENARIOINDEX))
            {
                datamgm = new TestScenarioConfigurationManager(LayoutConfigurationManager.GetInstance());
                WorkFlowScriptIDDataList[i].Id = node;
                WorkFlowScriptIDDataList[i].Name = datamgm.GetValue(node, "Name");
                WorkFlowScriptIDDataList[i].ParentId = sessionmgm.ScriptID;
            }
            else if (node.Contains(StatusCodes.TAC_TXT_SEQUENCEINDEX))
            {
                datamgm = new TestSequenceConfigurationManager(LayoutConfigurationManager.GetInstance());
                WorkFlowScriptIDDataList[i].Id = node;
                WorkFlowScriptIDDataList[i].Name = datamgm.GetValue(node, "Name");
                WorkFlowScriptIDDataList[i].ParentId = sessionmgm.ScriptID;
            }
            else if (node.Contains(StatusCodes.TAC_TXT_CASEINDEX))
            {
                datamgm = new TestCaseConfigurationManager(LayoutConfigurationManager.GetInstance());
                WorkFlowScriptIDDataList[i].Id = node;
                WorkFlowScriptIDDataList[i].Name = datamgm.GetValue(node, "Name");
                WorkFlowScriptIDDataList[i].ParentId = sessionmgm.ScriptID;
            }
            else
            {
                datamgm = new TestStepConfigurationManager(LayoutConfigurationManager.GetInstance());
                WorkFlowScriptIDDataList[i].Id = node;
                WorkFlowScriptIDDataList[i].Name = datamgm.GetValue(node, "Name");
                WorkFlowScriptIDDataList[i].ParentId = sessionmgm.ScriptID;
            }
            i++;
        }
        foreach (var item in WorkFlowScriptIDDataList)
        {
            localstore.Add(item);
        }
        while (StartWithTreeviewItem(localstore).Count > 0)
        {
            GetTreeviewItems(ref localstore);
        }
        //Creation -TreeView
        IEnumerable<WorkFlowScriptIDDataStore> data =  WorkFlowScriptIDDataList;
        var lookup = data.ToLookup(foo => foo.ParentId,
            foo => new TreeItem<object>() { Item = foo });
        foreach (var node in lookup.SelectMany(x => x))
            node.Children = lookup[((WorkFlowScriptIDDataStore)(node.Item)).Name];
        var rootNodes = lookup[null];
        treeView.ItemsSource = rootNodes;

}

private List<WorkFlowScriptIDDataStore>   StartWithTreeviewItem(List<WorkFlowScriptIDDataStore> lst)
    {
        return lst.Where(item => item.Id.StartsWith("TSC") ||
             item.Id.StartsWith("TSQ") ||  item.Id.StartsWith("TC")).ToList();
    }

private void GetTreeviewItems(ref List<WorkFlowScriptIDDataStore> nodeList)
    {
        List<string> tempIDList = new List<string>();
        List<string> tempPrereqList = new List<string>();
        string currentID = string.Empty;
        List<WorkFlowScriptIDDataStore> tempstore = new List<WorkFlowScriptIDDataStore>();

        configdoc = XDocument.Load(FilePaths.appWorkflowPath + "Workflow" + ".xml");
        for (int i = 0; i <= nodeList.Count; i++)
        {
            if (nodeList.Count > i)
            {
                if (nodeList.Count > 0)
                {
                    if (nodeList[i].Id.StartsWith("TSC") ||
                    nodeList[i].Id.StartsWith("TSQ") || nodeList[i].Id.StartsWith("TC"))
                    {
                        foreach (XElement xe in configdoc.Descendants("TAWorkFlow"))
                        {
                            if (nodeList[i].Id == xe.Element("UniqueID").Value)
                            {
                                tempIDList = xe.Element("ScriptID").Value.Split(new char[] { ',' },
                                        StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                                tempPrereqList = xe.Element("Prerequisite").Value.Split(new char[] { ',' },
                                        StringSplitOptions.RemoveEmptyEntries).ToList<string>();
                                break;
                            }
                        }

                        currentID = nodeList[i].Id;
                        tempIDList.Reverse();
                        tempPrereqList.Reverse();
                        int k = 0;
                        tempstore.Clear();
                        nodeList.RemoveAt(i);
                        foreach (string id in tempIDList)
                        {
                            tempstore.Add(new WorkFlowScriptIDDataStore());
                            tempstore[k].Id = id;
                            tempstore[k].Name = tempPrereqList[k];
                            tempstore[k].ParentId = currentID;
                            int j = i;
                            WorkFlowScriptIDDataList.Insert(j + 1, tempstore[k]);
                            nodeList.Insert(j, tempstore[k]);
                            j++;
                            k++;
                        }
                    }                      
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

您需要在WorkFlowScriptIDDataStore类中使用属​​性而不是变量。请尝试以下示例。

 <TreeView x:Name="treeView">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:WorkFlowScriptIDDataStore}" ItemsSource="{Binding Subcategories}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        WorkFlowScriptIDDataStore workFlowScriptIdDataStore = new WorkFlowScriptIDDataStore()
        {
            Id="1",
            Name = "Name",
            ParentId = "-1",
            Subcategories = new List<WorkFlowScriptIDDataStore>()
            {
                new WorkFlowScriptIDDataStore()
                {
                    Id="1",
                    Name = "Name23",
                    ParentId = "-1",
                },
                new WorkFlowScriptIDDataStore()
                {
                    Id="2",
                    Name = "Name1",
                    ParentId = "-1",
                },
            }
        };

        List<WorkFlowScriptIDDataStore> lst = new List<WorkFlowScriptIDDataStore>() { workFlowScriptIdDataStore };

        treeView.ItemsSource = lst;
    }
}

public class WorkFlowScriptIDDataStore
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string ParentId { get; set; }

    public List<WorkFlowScriptIDDataStore> Subcategories { get; set; }
    public void Clear()
    {
        Id = "";
        Name = "";
        ParentId = "";
    }
}