| Property | Description |
| Child | Represents the single child of a Viewbox element. |
| Stretch | Describes how content is resized to fill its allocated space. It has value None, Fill, Uniform, and UniformToFill. |
| StretchDirection | Gets or sets the StretchDirection, which determines how scaling is applied to the contents of a Viewbox. It has values Both, DownOnly, and UpOnly. |
- <Viewbox />
- <Viewbox Height="200" HorizontalAlignment="Left" Margin="19,45,0,0"
- Name="viewbox1" VerticalAlignment="Top" Width="300"
- Stretch="Fill">
- <Ellipse Width="100" Height="100" Fill="Red" />
- </Viewbox>
- <Window x:Class="WPFViewBox.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:WPFViewBox"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="800">
- <Grid Name="RootLayout">
- <Viewbox Height="200" HorizontalAlignment="Left" Margin="19,45,0,0"
- Name="viewbox1" VerticalAlignment="Top" Width="300"
- Stretch="Fill">
- <Ellipse Width="100" Height="100" Fill="Red" />
- </Viewbox>
- </Grid>
- </Window>
- private void CreateViewboxDynamically()
- {
- // Create a Viewbox object
- Viewbox dynamicViewbox = new Viewbox();
- // Set StretchDirection and Stretch properties
- dynamicViewbox.StretchDirection = StretchDirection.Both;
- dynamicViewbox.Stretch = Stretch.Fill;
- dynamicViewbox.MaxWidth = 300;
- dynamicViewbox.MaxHeight = 200;
- // Create an Ellipse dynamically
- Ellipse redCircle = new Ellipse();
- redCircle.Height = 100;
- redCircle.Width = 100;
- redCircle.Fill = new SolidColorBrush(Colors.Red);
- // Set Viewbox.Child to Ellipse
- dynamicViewbox.Child = redCircle;
- // Add Viewbox to Grid panel's child
- RootLayout.Children.Add(dynamicViewbox);
- }
- public MainWindow()
- {
- InitializeComponent();
- CreateViewboxDynamically();
- }
- private void CreateViewboxDynamically()
- {
- // Create a Viewbox object
- Viewbox dynamicViewbox = new Viewbox();
- // Set StretchDirection and Stretch properties
- dynamicViewbox.StretchDirection = StretchDirection.Both;
- dynamicViewbox.Stretch = Stretch.Fill;
- dynamicViewbox.MaxWidth = 300;
- dynamicViewbox.MaxHeight = 200;
- // Create an Ellipse dynamically
- Ellipse redCircle = new Ellipse();
- redCircle.Height = 100;
- redCircle.Width = 100;
- redCircle.Fill = new SolidColorBrush(Colors.Red);
- // Set Viewbox.Child to Ellipse
- dynamicViewbox.Child = redCircle;
- // Add Viewbox to Grid panel's child
- RootLayout.Children.Add(dynamicViewbox);
- }

Sachin VarkeyPosted Jun 8, 2021, 1:23 AM EDT
Hi, I have a viewbox with Grid as child. This Grid is separated into columns. Each columns size is * .If I place a gridsplitter in between this grid, the grid splitter is not working.But if I keep the size of grid as Auto then grid splitter is working fine.What could be the issue?
Yegor IusPosted Mar 12, 2020, 3:47 PM EDT
Hi! Looking for a way to control the shape of the ViewBox. E.g. is it possible to have a circular one, such that any zoom-in and zoom-out image manipulation stays with the circular boundary?
Yogi SPosted Nov 6, 2018, 6:06 AM EST
Very good Viewbox tutorial. Thank you