Why orders of child nodes in a treelayout differs?


Hi, I’m using TreeLayout and visualtreeModel to display my CustomType data. for example , there is a root data A , and has 3 childred, B, C ,D . when I click a button, the code below executes.

        Xmind.opes.Clear(); // opes is an observableCollection
        Xmind.opes.Add((CustomType ) A);
        GetChild(Xmind.opes,  A);
        msgope.m_Diagram.Model.NodesSource = Xmind.opes;

the GetChild method is like this
private void GetChild(ObservableCollection nodelist, CustomType rootData)
foreach(CustomType child in rootData.ChildList)
if(child.ChildList.Count>0) { nodelist.Add(child); GetChild(nodelist, child); }
else nodelist.Add(child);

the tree should be like
but the truth is, sometimes ,it turns like this

I wonder how can I fix the order of child nodes as they are orginazed.
I already used the customed Comparer and set the TreeLayout.Sorting=“Forwards” , but it did not work.


The Comparer is the only way to order the children of a node/vertex. What Comparer are you using?


I want to the children in order just as they are added into the datasource. so I used a comparer like below
public class SimpleComparer:IComparer
publis int Comparer(TreeVertex x, TreeVertex y)
CustomType a= x.Node.Data as CustomType ;
CustomType b= y.Node.Data as CustomType ;
int num1 = a.Parent.ChildList.IndexOf(a);
int num2 = b.Parent.ChildList.indexOf(b);
return num1.CompareTo(num2);

And it Compares the children of one same parent right?


Sorry, I missed that you are using Sorting=“Forwards”, which does not use the Comparer – it puts them in the order that they appear as children of the parent vertex.

Which is what you want, apparently. But if they are not in the right order, then you might not be adding the links from the parent node to the children in the desired order.


Thank you Walter, for your reply. I tried Sorting=“Forwards”, but the order of children was sometimes wrong sometimes right (). Then I try Sorting=“Ascending” Comparer="{StaticResource theSimpleComparer}", the tree looks fine. I never add a link in code, just add or delete nodedata into/from the node Source collection.