How do I force a GameObject with children to collapse and expand in the hierarchy view?

I’m using a C# script on a GameObject that has a check-box to hide and unhide the children of the GameObject. I also want the check-box to force the GameObject to collapse and expand in the hierarchy view when it is checked and unchecked.

How can I accomplish this effect? C# is my preferred language, but I’ll take any help I can get.

Alt-Click on the arrow next to it.