Adding and Removing Visual Children
Introduction
Every control in Gum has a Visual property which defines its appearance and size. Some controls have simple Visuals, such as a single ContainerRuntime as the Visual for StackPanel. Other controls are made of multiple children, such as the Button containing a top-level ContainerRuntime with a NineSliceRuntime and TextRuntime.
This document shows how children can be added or removed from a control's Visuals to customize its appearance.
For information on working with standard visuals, see the Standard Visuals section.
Adding Children Visuals
Additional children can be added directly to a control's Visual (top visual), or as a child of existing children.
The following code shows how to add a colored rectangle to a Button.
var button = new Button();
button.AddToRoot();
button.Anchor(Anchor.Center);
var buttonVisual = (ButtonVisual)button.Visual;
var coloredRectangle = new ColoredRectangleRuntime();
buttonVisual.AddChild(coloredRectangle);
coloredRectangle.Color = Color.Red;
coloredRectangle.Anchor(Anchor.Left);
coloredRectangle.X = 8;
coloredRectangle.Width = 8;
coloredRectangle.Height = 8;
As mentioned above, new children can be added directly to the root, or they can be added as children of existing children. For example, the following code could be used to underline text on a Button:

Removing Children Visuals
Gum controls are very flexible and can function even if children are removed. Of course, removing children may limit the behavior of a control. For example, removing the TextInstance from a ButtonVisual results in the Button no longer displaying its Text string.

Of course, the TextIntance can also be made invisible, which results in similar behavior:
Replacing Children with Different Types
Default visual children can be replaced with children of different types to further customize controls. For example, the Button control uses a NineSlice for its background, but this can be replaced with other types such as ColoredRectangleRuntime or SpriteRuntime.
The following code shows how to replace a button's default background with a SpriteRuntime. For simplicity this button uses Lorem Picsum.

Last updated
Was this helpful?

