ScrollViewer

Introduction

The ScrollViewer control provides a container which can hold Gum objects (including other Gum Forms objects). The user can scroll the ScrollViewer with the mouse or ScrollBar.

By default the ScrollViewer's InnerPanel expands automatically in response to its children and stacks its children top-to-bottom. Of course, this behavior can be changed since the InnerPanel is a standard GraphicalUiElement.

Code Example: Creating a ScrollViewer with Forms Children

ScrollViewers can contain other forms controls. The following code creates a ScrollViewer and adds buttons using the AddChild method.

// Initialize
var scrollViewer = new ScrollViewer();
scrollViewer.AddToRoot();
scrollViewer.X = 50;
scrollViewer.Y = 50;
scrollViewer.Width = 200;
scrollViewer.Height = 200;
scrollViewer.InnerPanel.StackSpacing = 2;

for (int i = 0; i < 30; i++)
{
    var button = new Button();
    scrollViewer.AddChild(button);
    button.Text = "Button " + i;
    button.Click += (_, _) =>
        button.Text = DateTime.Now.ToString();
}

Try on XnaFiddle.NETarrow-up-right

Children in a ScrollViewer

Scrolling a ScrollViewer

ScrollViewers can be scrolled by the user using by performing any of the following actions:

  • Clicking on the ScrollBar buttons or track

  • Dragging the ScrollBar thumb

  • Using the mouse wheel to scroll vertically

  • Using shift+mouse wheel to scroll horizontally. For more information see the Horizontal Scrolling page.

Code Example: Creating a ScrollViewer With Non-Forms Children

The following code creates a ScrollViewer and adds ColoredRectangleRuntimes to the ScrollViewer. Any non-Forms visual object can be added to the ScrollViewer through AddChild.

Try on XnaFiddle.NETarrow-up-right

ScrollViewer displaying multiple ColoredRectangles

Code Example: Wrapping Children

The following code shows how to wrap children in a ScrollViewer. It modifies the InnerPanel to change the layout type.

Try on XnaFiddle.NETarrow-up-right

ScrollViewer with wrapped children

Code Example: Filling Available Space

The following code creates a ScrollViewer that fills the entire screen using RelativeToParent sizing. The code also subscribes to Window.ClientSizeChanged to update the Gum canvas and layout when the window is resized. For more information on handling resizes, see the Resolution and Resizing the Game Window page.

Try on XnaFiddle.NETarrow-up-right

Last updated

Was this helpful?