Recursively Add Items to a TreeView
<p>I have a TreeView imported from WinRT.XamlToolKit that I'm trying to use in a Universal App. It's important to note, I understand there are similar questions, but some don't work either because they predate Universal Apps, or they don't accommodate the usage of MVVM. Following a tutorial to understand how to populate the tree, I came across this recursive method:</p>

<pre><code>private ObservableCollection&lt;TreeViewItemModel&gt; BuildTree(int depth, int branches)
    {
        var tree = new ObservableCollection&lt;TreeViewItemModel&gt;();

        if (depth &lt;= 0) return tree;
        var depthIndices = Enumerable.Range(0, branches).Shuffle();

        for (var i = 0; i &lt; branches; i++)
        {
            var d = depthIndices[i] % depth;
            var b = _rand.Next(branches / 2, branches);
            tree.Add(
                new TreeViewItemModel
                {
                    Branch = b,
                    Depth = d,
                    Text = "Item " + _itemId++,
                    Children = BuildTree(d, b)
                });
        }
        return tree;
    }
</code></pre>

<p>It's called as such <code>TreeItems = BuildTree(5,5);</code>  I then bind my TreeView ItemSource to TreeItems and attach to the Text property of the TreeViewItemModel.</p>

<p>This works for a pre-determined set, and then as seen in the method, uses random numbers to work out how the tree will get displayed. (In what depths, how many branches, etc.)</p>

<p>What I'm not sure of is how to implement a similar recursive function to populate the tree with a different ItemModel with an undetermined number of children.  In my setup, I have a Space, that can have children Spaces, Devices, and/or Sensors.  A space could have no devices and no sensors, but one or more child spaces, or it could have no child spaces, but it could have devices and/or sensors. In my Sensor class it has a Parent Id relevant to the Id of the Space it's bound to and the Device it reports to.</p>

<p>So, in short, I need to populate my <code>ObservableCollection&lt;TreeViewSpaceModel&gt;</code> recursively from a list of Spaces.</p>

<p>TreeViewSpaceModel class:</p>

<pre><code>public class TreeViewSpaceModel : NotifyObject
{
    private string _name;
    private Guid _iD;
    private string _parentName;
    private ObservableCollection&lt;TreeViewSpaceModel&gt; _children = new ObservableCollection&lt;TreeViewSpaceModel&gt;();
    private ObservableCollection&lt;TreeViewDeviceModel&gt; _devices = new ObservableCollection&lt;TreeViewDeviceModel&gt;();
    private ObservableCollection&lt;TreeViewSensorModel&gt; _sensors = new ObservableCollection&lt;TreeViewSensorModel&gt;();

    public string Name
    {
        get { return _name; }
        set { SetProperty(ref _name, value); }
    }

    public string ParentName
    {
        get { return _parentName; }
        set { SetProperty(ref _parentName, value); }
    }

    public Guid Id
    {
        get { return _iD; }
        set { SetProperty(ref _iD, value); }
    }

    public ObservableCollection&lt;TreeViewSpaceModel&gt; Children
    {
        get { return _children; }
        set { SetProperty(ref _children, value); }
    }

    public ObservableCollection&lt;TreeViewDeviceModel&gt; Devices
    {
        get { return _devices; }
        set { SetProperty(ref _devices, value); }
    }

    public ObservableCollection&lt;TreeViewSensorModel&gt; Sensors
    {
        get { return _sensors; }
        set { SetProperty(ref _sensors, value); }
    }
}
</code></pre>

<p>The DeviceModel has no Children but it does have Sensors, and the SensorModel has no Children or Devices or Sensors, as it is the end of the hierarchy.
Space class:</p>

<pre><code>public class Space
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public int SpaceTypeId { get; set; }
    public virtual Space Parent { get; set; }
    public virtual ICollection&lt;Sensor&gt; Sensors { get; set; }
    public virtual ICollection&lt;Device&gt; Devices { get; set; }
    public virtual ICollection&lt;Space&gt; Children { get; set; }
}
</code></pre>

<p>The end result would ideally look something like this:</p>

<pre><code>Space1
---- ChildSpace1
--------Device1
-------------Sensor1
-------------Sensor2 (Sensors report for Spaces, but are bound to Devices)
--------SubChildSpace1
-------------Sensor1
-------------Sensor2
--------SubChildSpace2
-------------Sensor1
------------Sensor2
--------Device2
---- ChildSpace2
Space2
</code></pre>

<p>If there's an easier way to do this instead of a recursive method, I'm happy to entertain it.  While researching TreeViews for Universal Apps, I did come across someone else saying the user should be able to do something like <code>items.Node(0).Add(stuffhere)</code>;however, I could not figure out how to achieve this as an ObservableCollection didn't offer the .Node extension.</p>