A
A
Arseniy2014-05-26 21:14:42
WPF
Arseniy, 2014-05-26 21:14:42

Is it possible to convert such a class to a TreeView?

There is a class like this:

public class Block
{
        public string title; // название класса
        public Dictionary<string, object> property; // свойства, object может быть string или List<string>
        public Block parent; // ссылка на блок родителя
}

In essence, this is a display of the JSON format, for example, this JSON :
{
  "query_block": {
    "select_id": 1,
    "table": {
      "table_name": "firmsmaterials",
      "access_type": "ALL",
      "rows": 11,
      "filtered": 100
    }
  }
}

will be the following set of Block
1 class objects: Block 1
title = query_block
poperties = "select_id : 1"
parent = null
2: Block 2
title = table
properties =...
parent = Block 1
etc...
In general, where does " {" a new block is created with a reference to the parent (this was originally intended to draw JSON block diagrams). One way or another, I get a List.
I wanted to convert this List into a TreeView, something like this:
query_block >
...select_id : 1
...table>
......"table_name": "firmsmaterials",
......"access_type": " ALL",
......"rows": 11,
......
And so on ad infinitum. I tried to write a code that would convert a List to a TreeView, but it does not work at all as I want. To be honest, I don't know what to do anymore.
StringBuilder arrProperties;
            TreeViewItem tvItem, ptvItem = new TreeViewItem { Header = "root"};
            foreach (Block b in blocks)
            {
                
                tvItem = new TreeViewItem { Header = b.title };
                
                foreach(KeyValuePair<string, object> i in b.property)
                {
                    if (i.Value is string || i.Value is int)
                    {
                        tvItem.Items.Add(new TreeViewItem { Header = i.Key + " : " + i.Value });
                    }
                    else if (i.Value is List<string>)
                    {
                        arrProperties = new StringBuilder();
                        arrProperties.Append(" [ ");
                        foreach (string item in (List<string>)i.Value)
                        {
                            arrProperties.Append(item + ", ");
                        }
                        arrProperties.Remove(arrProperties.Length - 2, 2);
                        arrProperties.Append(" ]");
                        tvItem.Items.Add(new TreeViewItem { Header = i.Key + " : " + arrProperties.ToString() });
                    }
                }
                ptvItem.Items.Add(tvItem);
                ptvItem = tvItem;
                
            }
            trvQueryPlan.Items.Add(ptvItem);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question