private void CollapseExpandButton_Click(object sender, RoutedEventArgs e) { // the Button is in the visual tree of a Node Button button = (Button)sender; Node n = Part.FindAncestor <Node>(button); if (n != null) { SimpleData parentdata = (SimpleData)n.Data; // always make changes within a transaction myDiagram.StartTransaction("CollapseExpand"); // if needed, create the child data for this node if (!parentdata.EverExpanded) { parentdata.EverExpanded = true; // only create children once per node! int numchildren = CreateSubTree(parentdata); if (numchildren == 0) // now known no children: don't need Button! { button.Visibility = Visibility.Collapsed; } } // toggle whether this node is expanded or collapsed n.IsExpandedTree = !n.IsExpandedTree; if (n.IsExpandedTree) { myDiagram.Panel.CenterPart(n); } else { myDiagram.Panel.CenterPart(n.NodesInto.FirstOrDefault()); } myDiagram.CommitTransaction("CollapseExpand"); } }
private int CreateSubTree(SimpleData parentdata) { int numchildren = rand.Next(10); if (myDiagram.PartManager.NodesCount <= 1) { numchildren += 1; // make sure the root node has at least one child } // create several SimpleData objects and add them to the model for (int i = 0; i < numchildren; i++) { SimpleData childdata = new SimpleData(); childdata.Color = String.Format("#{0:X}{1:X}{2:X}", 120 + rand.Next(100), 120 + rand.Next(100), 120 + rand.Next(100)); childdata.Key = parentdata.Key + "/" + childdata.Color; childdata.ParentKey = parentdata.Key; myDiagram.Model.AddNode(childdata); } return(numchildren); }