Viewbox.Child Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the single child element of a Viewbox element.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Property Child As UIElement
public UIElement Child { get; set; }
<ViewBox ...>
  child
</ViewBox>

XAML Values

  • child
    A single object element of a type that derives from UIElement, which is contained by this Viewbox.

Property Value

Type: System.Windows.UIElement
The single child element of a Viewbox element.

Remarks

A Viewbox can have only one Child. If you add an additional Child, an ArgumentException will be thrown at run time. If you want to add multiple controls to a Viewbox, add those controls to a panel element, such as a StackPanel, Grid, or Canvas, and then add the panel element to the Viewbox.

Examples

The following example adds two images to a Viewbox control by adding the images to a StackPanel, and then adding the StackPanel to the Viewbox.

<Grid x:Name="LayoutRoot" Background="White">
    <Viewbox Height="500" Width="600">
        <StackPanel Orientation="Horizontal">
            <Image Source="flower.jpg" />
            <Image Source="licorice.jpg" />
        </StackPanel>
    </Viewbox>
</Grid>
private void DisplayViewBox()
{
    Viewbox MyVB = new Viewbox();

    //Setting the Height and Width of the ViewBox
    MyVB.Height = 500;
    MyVB.Width = 600;

    //Defining two image elements
    Image MyImage1 = new Image();
    MyImage1.Source = new BitmapImage(new Uri("flower.jpg", UriKind.RelativeOrAbsolute));
    Image MyImage2 = new Image();
    MyImage2.Source = new BitmapImage(new Uri("licorice.jpg", UriKind.RelativeOrAbsolute));

    //Adding the Image elements to a StackPanel
    StackPanel MySP = new StackPanel();
    MySP.Orientation = Orientation.Horizontal;
    MySP.Children.Add(MyImage1);
    MySP.Children.Add(MyImage2);

    //Adding the StackPanel to the ViewBox
    MyVB.Child = MySP;

    //Adding the ViewBox to the Grid
    LayoutRoot.Children.Add(MyVB);

}
Private Sub DisplayViewBox()
    Dim MyVB As New Viewbox()

    'Setting the Height and Width of the ViewBox
    MyVB.Height = 500
    MyVB.Width = 600

    'Defining two image elements
    Dim MyImage1 As New Image()
    MyImage1.Source = New BitmapImage(New Uri("flower.jpg", UriKind.RelativeOrAbsolute))
    Dim MyImage2 As New Image()
    MyImage2.Source = New BitmapImage(New Uri("licorice.jpg", UriKind.RelativeOrAbsolute))

    'Adding the Image elements to a StackPanel
    Dim MySP As New StackPanel()
    MySP.Orientation = Orientation.Horizontal
    MySP.Children.Add(MyImage1)
    MySP.Children.Add(MyImage2)

    'Adding the StackPanel to the ViewBox
    MyVB.Child = MySP

    'Adding the ViewBox to the Grid

    LayoutRoot.Children.Add(MyVB)
End Sub

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.