Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Adding child to node.

$
0
0

Hi, i have a treeview in silverlight.

I can add the root with child nodes by doing like this

            Thing st = new Thing();
            st.StatusBrush = new SolidColorBrush(Colors.Purple);
            st.Name = strSubNodes[2].ToString();
            tvi.Children.Add(st);
            st.Children.Add(
              new Thing { StatusBrush = new SolidColorBrush(Colors.Purple), Name = strSubNodes[2].ToString() }
              );

The Thing looks like this:

    public class Thing
    {
        public SolidColorBrush StatusBrush { get; set; }
        public string Name { get; set; }
        public ObservableCollection<Thing> Children { get; set; }
    }

But when i add the root nodes first, and later want to add child nodes to the various root nodes, i have yet to find a way to add child nodes to the root. Latest try is this code. but the Children is null, so i cannot add to it (NullPointerReference)

        private void AddNode(string strNode, string[] strSubNodes)
        {

            Thing tvi = FindNode(strNode);

            if (tvi == null)
            {
                AddRoot(strNode);
                tvi = FindNode(strNode);
            }


            foreach (Thing rti in tv.Items)
            {
                //tb3.Text += "rti: " + rti.Name + "\n";
                if (rti.Name == strNode)
                {

                    rti.Children.Add(
                new Thing { StatusBrush = new SolidColorBrush(Colors.Purple), Name = strSubNodes[2].ToString() }
                );
                    return;
                }
            }
        }

        private Thing FindNode(string strNode)
        {
            foreach (Thing tvi in tv.Items)
            {
                if (tvi.Name == strNode)
                {
                    return tvi;
                }
            }

            return null;
        }



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>