# Viewbox

The `Viewbox` is a decorator control which scales its child. It can be used to scale its child to fit the available space.

The `Viewbox` gives its child infinite space in the measure phase. It will constrain either or both sides when arranging it. This depends on the value of the `Stretch`.

To restrict scaling direction one can use `StretchDirection` which can prevent up or down scaling.

{% tabs %}
{% tab title="XAML" %}

```markup
<!-- Ellipse will occupy 50x50px space -->
<Ellipse Width="50" Height="50" Fill="CornflowerBlue" />  

<!-- Ellipse will be scaled to occupy 300x300px space -->
<Viewbox Stretch="Uniform" Width="300" Height="300">
	<Ellipse Width="50" Height="50" Fill="CornflowerBlue" />  
</Viewbox>
```

{% endtab %}

{% tab title="C#" %}

```csharp
// Ellipse will occupy 50x50px space
new Ellipse
{
	Width = 50,
	Height = 50,
	Fill = Brushes.CornflowerBlue
};

// Ellipse will be scaled to occupy 300x300px space
new Viewbox
{
	Stretch = Stretch.Uniform,
	Width = 300,
	Height = 300,
	Child = new Ellipse
	{
		Width = 50,
		Height = 50,
		Fill = Brushes.CornflowerBlue
	}
};
```

{% endtab %}
{% endtabs %}

### Common Properties

| Property           | Type                                                                                    | Default | Description                                        |
| ------------------ | --------------------------------------------------------------------------------------- | ------- | -------------------------------------------------- |
| `Stretch`          | [Stretch](http://reference.avaloniaui.net/api/Avalonia.Media/Stretch)                   | Uniform | Determines how child fits into the available space |
| `StretchDirection` | [StretchDirection](http://reference.avaloniaui.net/api/Avalonia.Media/StretchDirection) | Both    | Determines in what direction child will be scaled  |

### Examples

| `Stretch`       |                                                                                                                                                                                                                                                            |
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Uniform`       | <img src="https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-bd97c5b559aa4e8df060a15fd7964169d3ee77eb%2Fscale-uniform-both.gif?alt=media" alt="" data-size="original">       |
| `UniformToFill` | <img src="https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-1597231ed9ae6fe9163a74ce7a9ff515402a120a%2Fscale-uniformtofill-both.gif?alt=media" alt="" data-size="original"> |
| `Fill`          | <img src="https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-908b89e27ce0a3a2835aed83b474b26f55f033d4%2Fscale-fill-both.gif?alt=media" alt="" data-size="original">          |
| `None`          | <img src="https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-20880997b7eedaaf01b7dbedb771b67d6a3f447f%2Fscale-none-both.gif?alt=media" alt="" data-size="original">          |

| `StretchDirection` |                                                                                                                                                                                                                                                          |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Both`             | ![](https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-bd97c5b559aa4e8df060a15fd7964169d3ee77eb%2Fscale-uniform-both.gif?alt=media)                                        |
| `UpOnly`           | <img src="https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-5f196c215af152a322a2ebfeb91fd142ab1e5a24%2Fscale-uniform-uponly.gif?alt=media" alt="" data-size="original">   |
| `DownOnly`         | <img src="https://4025525846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9CodmwWc6hOAzwuMGtm4%2Fuploads%2Fgit-blob-fd0446c269b26fb05cfecfdc593fe12fac7ccf94%2Fscale-uniform-downonly.gif?alt=media" alt="" data-size="original"> |

### Pseudoclasses <a href="#pseudoclasses" id="pseudoclasses"></a>

None

### Reference <a href="#reference" id="reference"></a>

[Viewbox](http://reference.avaloniaui.net/api/Avalonia.Controls/Viewbox/)

### Source code <a href="#source-code" id="source-code"></a>

[Viewbox.cs](https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Controls/Viewbox.cs)
