> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/styling/code-only-styling/customizing-menu-and-menuitem.md).

# Customizing Menu and MenuItem

## Introduction

MenuItems can contain sub-items. This page discusses how to customize individual MenuItem instances as well as the container that holds MenuItem instances (ScrollViewer).

## Customizing MenuItem Text Color

```csharp
// Initialize
var menu = new Menu();
menu.AddToRoot();

var fileMenuItem = new MenuItem();
menu.Items.Add(fileMenuItem);
fileMenuItem.Header = "File";

var random = new System.Random();

for (int i = 0; i < 10; i++)
{
    var item = new MenuItem();
    item.Header = "File " + i;
    var itemVisual = (MenuItemVisual)item.Visual;
    // Remove the color values from the states so they can
    // be set directly
    itemVisual.ForegroundColor = new Color(
        100 + random.Next(155),
        100 + random.Next(155),
        100 + random.Next(155));

    fileMenuItem.Items.Add(item);
}
```

<figure><img src="/files/CcQT5T7jKR4BFsVM1jOM" alt=""><figcaption></figcaption></figure>

For more information on working with `TextRuntime` instances, see the [TextRuntime page](/gum/code/standard-visuals/textruntime.md).

## Customizing Sub-Item Background (ScrollViewer)

Each `MenuItem` can contain sub-items which appear when the user hovers over the `MenuItem`. These sub-items are shown as children of a `ScrollViewer` instance. This pop-up `ScrollViewer` can be customized by assigning the `MenuItem.ScrollViewerVisualTemplate` property.

The following code shows how to customize the `ScrollViewer` so it has a red background.

```csharp
// Initialize
var menu = new Menu();
menu.AddToRoot();

var menuItem = new MenuItem();
menu.Items.Add(menuItem);
menuItem.ScrollViewerVisualTemplate = new VisualTemplate(() =>
{
    var visual = new ScrollViewerVisual();
    visual.BackgroundColor = Color.Red;
    return visual;
}); 

menuItem.Header = "File";

for( int i = 0; i < 10; i++)
{
    var subItem = new MenuItem();
    subItem.Header = $"Sub Item {i + 1}";
    menuItem.Items.Add(subItem);
}
```

<figure><img src="/files/qEe7S28LcpJPTY0loDDF" alt=""><figcaption><p>File menu displaying sub-items on a red background</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/gum/code/styling/code-only-styling/customizing-menu-and-menuitem.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
