# ItemsControl

## Introduction

`ItemsControl` provides a way to display a collection of controls. By default all contained controls are stacked and allow scrolling with a ScrollBar.

{% hint style="info" %}
ItemsControl is similar to ListBox, but it is more general since it does not support selection. When items are added to the Items property the ItemsControl does not create a control which inherits from ListBoxItem. Similarly, its VisualTemplate does not need to be a type which supports a ListBoxItem.
{% endhint %}

ItemsControl inherits from ScrollViewer. For more information about inherited proerties, see the [ScrollViewer](https://docs.flatredball.com/gum/code/controls/scrollviewer) page.

## Code Example: Adding to Items Property

If an object is added to the `Items` property then the `ItemsControl` creates a view to represent this. By default this is a `Label` instance, but it can be customized through the `VisualTemplate` property.

The following code creates ten `Labels` by adding integers to the `Items` property.

```csharp
// Initialize
var itemsControl = new ItemsControl();
itemsControl.AddToRoot();
for(int i = 0; i < 10; i++)
{
    itemsControl.Items.Add(i);
}
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAACqvm5VJQUPIsdi_NVbJSKCkqTdUBi2TmZZZkJuZkVqUChZXKEosUMktSc4ud8_NKivJzFGwV8lLLFTyRhDQ0rWOAehACeo4pKSH5Qfn5JWCptPwijcy8EoVMoF4DayBlo2AIorW1NWPyqmPyFIAARTvYcJAhGpkg_bVKvFy1vFwA64IJNbAAAAA)

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-95ebaf68ed3abe73256a41dcef5f786c635d55ae%2F19_05%2028%2054.png?alt=media" alt=""><figcaption><p>ItemsControl with ten items</p></figcaption></figure>

## Code Example: Fixed-Size Scrollable List

Setting explicit `Width` and `Height` values gives the ItemsControl a fixed size so that its content scrolls when it overflows. This is useful for displaying a non-selectable scrollable list in a fixed area.

```csharp
// Initialize
var itemsControl = new ItemsControl();
itemsControl.AddToRoot();
itemsControl.Width = 200;
itemsControl.Height = 150;
for(int i = 0; i < 20; i++)
{
    itemsControl.Items.Add("Item " + i);
}
```

[Try on XnaFiddle.NET](https://xnafiddle.net/#snippet=H4sIAAAAAAAACmWOPwvCMBDF90K_w5EppSJRcLE6iIN2FcElSyHRHrQJpFcFS7-7ly7-u-Udv8e7e0OaAIiyO_StWAOF3s4mgg4JqwaflrG4VwGQbNvtvaPgG9iCsw8oP5DMCs2ZN5jvjDn7k_f0b13QUM1Hlkr9WkeLt5rYW6yid_VBoiNAJqpg2XCINc8z7QbtgOcrP3WKv6UWcQctIAeMFUaRJuML5gZQPvEAAAA)
