1

I have a list of objects that refers the ID, Name and ParentID. I tried to create the treeview. But it is not displaying anything. Could you please help me to create a treeView.I am sharing the code below.

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 Answer 1

2

You need to use properties instead of variables in the WorkFlowScriptIDDataStore class.Try the below sample.

 <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 = "";
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Ayyappan. But unfortunately it is not working. Here it is displaying like list.In this each node has a Parent Id. It it should come under that particular node.
@AnuHardin I tried its working, Not sure able about how you tried. Make sure you build the WorkFlowScriptIDDataStore properly. It shd work,
@Ayyappan.This is working only for one level of childs.If a child having subchilds it won't work.
I have added child item as Sub Category of Child.Now it is working.Thanks Ayyappan.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.