This is a migrated thread and some comments may be shown as answers.

Initializing Children for HierarchicalNodeViewModel

3 Answers 80 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Matthias
Top achievements
Rank 1
Matthias asked on 19 Apr 2017, 08:57 PM

Can somebody point me towards examples for initializing the child/parent relationship for diagram nodes derived from HierarchicalNodeViewModel? This example code from the MVVM serialization example (http://docs.telerik.com/devtools/wpf/controls/raddiagram/howto/raddiagrams-serialize-databound-diagram) results in a Children property with 0 entries for the rootNode:

private void BindGraphSource()
{
    int uniqueIdCounter = 0;
    GraphSource source = new GraphSource();
    OrgItem rootItem = new OrgItem() { Title = "CEO", Position = new Point(200, 20), Id = (uniqueIdCounter++).ToString() };
    source.AddNode(rootItem);
 
    OrgItem unitOne = new OrgItem() { Title = "Unit Manager USA", Position = new Point(100, 100), Id = (uniqueIdCounter++).ToString() };
    source.AddNode(unitOne);
    source.AddLink(new OrgLink(rootItem, unitOne) { Id = (uniqueIdCounter++).ToString() });
 
    OrgItem unitTwo = new OrgItem() { Title = "Unit Manager Europe", Position = new Point(300, 100), Id = (uniqueIdCounter++).ToString() };
    source.AddNode(unitTwo);
    source.AddLink(new OrgLink(rootItem, unitTwo) { Id = (uniqueIdCounter++).ToString() });
 
    this.xDiagram.GraphSource = source;
}

 

Manually setting the children doesn't seem straighforward, either: Even though the doc for the HierarchicalNodeViewModel.Children property reads "Gets or sets the children of the current node", the property only has a getter and no setter:

public ObservableCollection<HierarchicalNodeViewModel> Children { get; }

3 Answers, 1 is accepted

Sort by
0
Matthias
Top achievements
Rank 1
answered on 20 Apr 2017, 08:00 PM

To partially answer my own question, it looks like the only way to set children is at instance creation.

using System;
using System.Collections.ObjectModel;
 
namespace Telerik.Windows.Controls.Diagrams.Extensions.ViewModels
{
    /// <summary>
    /// MVVM representation of a hierarchical node.
    /// </summary>
    public class HierarchicalNodeViewModel : NodeViewModelBase
    {
        private readonly ObservableCollection<HierarchicalNodeViewModel> children;
 
        /// <summary>
        /// Gets or sets the children of the current node.
        /// </summary>
        /// <value>The children.</value>
        public ObservableCollection<HierarchicalNodeViewModel> Children
        {
            get
            {
                return this.children;
            }
        }
 
        /// <summary>
        /// Gets a value indicating whether this instance has children.
        /// </summary>
        /// <value>
        ///     <c>True</c> if this instance has children; otherwise, <c>false</c>.
        /// </value>
        public bool HasChildren
        {
            get
            {
                return this.Children.Count > 0;
            }
        }
 
        /// <summary>
        /// Initializes a new instance of the <see cref="T:Telerik.Windows.Controls.Diagrams.Extensions.ViewModels.HierarchicalNodeViewModel" /> class.
        /// </summary>
        public HierarchicalNodeViewModel()
        {
            this.children = new ObservableCollection<HierarchicalNodeViewModel>();
        }
    }
}

 

Which leads to a follow-up question: is there any built-in additional functionality for dependency graphs, or should I roll my own?

0
Accepted
Martin Ivanov
Telerik team
answered on 24 Apr 2017, 02:16 PM
Hello Matthias,

Even the Children collection has only a getter you can modify it. To do this you can remove and add items from the collection. So, for example, if you want to set new children, you can clear the already added ones and include the new child items.

Keep in mind that this collection is not internally used by the diagram or its layout mechanism. It is a helper that you can use to store the children of the parent node. The parent-child relation is create via linking nodes. In the provided code snippet, the following code creates the relation.
source.AddLink(new OrgLink(rootItem, unitOne) { Id = (uniqueIdCounter++).ToString() });
You can manually store this information in the Children collection of the rootItem.

Additionally, you can take a look at the SortTreeLayoutShapes SDK example that shows an example with hierarchical data.

Regards,
Martin
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Matthias
Top achievements
Rank 1
answered on 02 May 2017, 06:09 AM
Thanks, i solved this by extending the node. I don't know what I was smoking when thinking there was a setter :)
Tags
Diagram
Asked by
Matthias
Top achievements
Rank 1
Answers by
Matthias
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or