Collapsing and expanding Subgraph


I have a custom subgraph which can expand/ collapse having children as Custom GoNode objects which further can have children. I would like to know the best way to handle showing/hiding child items of subgraph on Expand/Collapse.
Currently I am overriding Collapse() and Expand() methods of GoSubgraph. I am storing the bounds of children and setting them to zero on collapse and setting back the original bounds on expand. Is this the right way to implement this. Do you suggest any other best way of hiding/showing child items?

I don’t understand – all this is done automatically by the standard implementations of GoSubGraph.Collapse and .Expand.

In other words, Collapse first Collapse()'s all children that are subgraphs. It also remembers whether each subgraph was expanded, if the Collapse call was needed.
And correspondingly, Expand calls Expand() on each child GoSubGraph that was expanded before the Collapse, and then restores its relative position.
So GoSubGraph already does what I believe you are asking for.
You can try this in SubGraphApp or Demo1.

Hello Walter,

I took a look at SubGraphApp demo code. I directly took TreeSubGraph and added an instance of GoRectanle of size 100X100 as a child. Now when I collapse, the GoRectangle disappears but GoSubgraph size remains almost same ( Size shrinks only little bit) . What I want is on collapse, only GoSubgraph handle and lable should be visible. I want to know why I am seeing empty subgraph with more space on collapse. Do I have to add some extra behavior to GoRectangle child in this case?
I'm assuming you aren't using a GoSubGraph.CollapsedObject.
If you want a particular size area when the subgraph is collapsed, you should override ComputeCollapsedSize to return the value you want, and you should override ComputeInsideMarginsSkip to ignore nodes when they are not Visible.
[code] // specify a constant size (inside margins) when collapsed
public override SizeF ComputeCollapsedSize(bool visible) {
return new SizeF(100, 50);
// ignore sizes of child nodes when they are not visible
protected override bool ComputeInsideMarginsSkip(GoObject child) {
if (!child.Visible) return true;
return base.ComputeInsideMarginsSkip(child);
} // position the Label next to the Handle, only when collapsed
public override void LayoutLabel() {
if (!this.IsExpanded) {
GoObject h = this.Handle;
if (h == null) return;
GoObject lab = this.Label;
if (lab == null) return;
lab.SetSpotLocation(TopLeft, h, TopRight);
} else {
The positioning of the Label doesn't always make sense when there isn't anything to position it with (and there isn't any CollapsedObject either), so I have overridden LayoutLabel to just put it next to the Handle. But LayoutLabel does the normal thing when the subgraph is expanded.

Hello Walter,

Thanks for your help. protected override bool ComputeInsideMarginsSkip(GoObject child) method call solved my problem.
I have one problem regarding position of subgraph label. I am using LayoutLabel() as suggested by you. At startup, Handle always hides the label of the subgraph. It is only after First time I collapse and then expand that the label moves to desired position. I tried to call LayoutLable() inside the costructor of subgraph, but it did not help. I tried setting location of label explicitly, that also did not help. Could you please suggest how to overcome this startup problem?

I’m sorry, but I can’t reproduce that behavior.

LayoutLabel() behaves in the standard manner when IsExpanded is true,
so I don't see how the behavior is any different without that override.