Styling Using States
Introduction
Gum controls respond to cursor and input actions by changing their appearance. States can be used to customize how controls react to these actions.
Each control provides states through its Visual object. Once a Visual is casted to its specific type, its states can be customized.
For more information about Visuals, see the Styling Individual Controls page.
Accessing Strongly-Typed States
The following table lists the states available for each control type:
ButtonVisual
Disabled
DisabledFocused
Enabled
Focused
Highlighted
HighlightedFocused
Pushed
CheckBoxVisual
DisabledOn / Off / Indeterminate
DisabledFocusedOn / Off / Indeterminate
EnabledOn / Off / Indeterminate
FocusedOn / Off / Indeterminate
HighlightedOn / Off / Indeterminate
HighlightedFocusedOn / Off / Indeterminate
PushedOn / Off / Indeterminate
ComboBoxVisual
Disabled
DisabledFocused
Enabled
Focused
Highlighted
HighlightedFocused
Pushed
ItemsControlVisual
Enabled
Focused
LabelVisual
<No States>
ListBoxItemVisual
Disabled
Enabled
Focused
Highlighted
Selected
ListBoxVisual
Disabled
DisabledFocused
Enabled
Focused
Highlighted
HighlightedFocused
Pushed
MenuItemVisual
Disabled
Enabled
Focused
Highlighted
Selected
MenuVisual
<No States>
PasswordBoxVisual
Disabled
Enabled
Focused
Highlighted
PasswordBoxVisual also includes states for single and multi-line layouts, but these states are not covered in this document.
RadioButtonVisual
DisabledOn / Off
DisabledFocusedOn / Off
EnabledOn / Off
FocusedOn / Off
HighlightedOn / Off
HighlightedFocusedOn / Off
Pushed On / Off
ScrollBarVisual
<No Color-related States> ScrollBar includes states for Vertical and Horizontal alignment, but these states are not covered in this document.
ScrollViewerVisual
Enabled
Focused
SliderVisual
Disabled
DisabledFocused
Enabled
Focused
Highlighted
HighlightedPushed
Pushed
SplitterVisual
<No States>
TextBoxVisual
Disabled
Enabled
Focused
Highlighted
TextBoxVisual also includes states for single and multi-line layouts, but these states are not covered in this document.
WindowVisual
<No States>
Code Example: Setting States on a ButtonVisual
To change how a control displays itself using states, the following steps must be performed:
Cast the Visual of the control to the appropriate Visual type
Clear the state that you would like to change
Assign the behavior of the state by setting its Apply or filling its Variables
The following code shows how to change a button so that its colors are explicitly set when highlighted, pressed, or in its default (Enabled) state.
var button = new Button();
button.AddToRoot();
button.Anchor(Anchor.Center);
var buttonVisual = (ButtonVisual)button.Visual;
var enabledState = buttonVisual.States.Enabled;
enabledState.Clear();
enabledState.Apply = () =>
{
buttonVisual.Background.Color = Color.Green;
};
var pushedState = buttonVisual.States.Pushed;
pushedState.Clear();
pushedState.Apply = () =>
{
buttonVisual.Background.Color = Color.DarkBlue;
};
var highlightedState = buttonVisual.States.Highlighted;
highlightedState.Clear();
highlightedState.Apply = () =>
{
buttonVisual.Background.Color = Color.Yellow;
};
// forcably apply the states to see the effect immediately
button.UpdateState();
Although the default states only modify color, custom states are free to modify any property on the Visual. For example, the following code shows how to also modify the button's text size and increase the background size.
var button = new Button();
button.AddToRoot();
button.Anchor(Anchor.Center);
var buttonVisual = (ButtonVisual)button.Visual;
var enabledState = buttonVisual.States.Enabled;
enabledState.Clear();
enabledState.Apply = () =>
{
buttonVisual.TextInstance.FontScale = 1;
buttonVisual.Background.Width = 0;
buttonVisual.Background.Height = 0;
};
var pushedState = buttonVisual.States.Pushed;
pushedState.Clear();
pushedState.Apply = () =>
{
buttonVisual.TextInstance.FontScale = 1;
buttonVisual.Background.Width = -4;
buttonVisual.Background.Height = -4;
};
var highlightedState = buttonVisual.States.Highlighted;
highlightedState.Clear();
highlightedState.Apply = () =>
{
buttonVisual.TextInstance.FontScale = 1.3f;
buttonVisual.Background.Width = 2;
buttonVisual.Background.Height = 2;
};
Any property on a control's Visual can be changed through state. For more information on working with the standard visual types which make up controls, see the Standard Visuals page.
Last updated
Was this helpful?

