Viewbox.Stretch Property

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

Gets or sets the Stretch mode, which determines how content fits into the available space.

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

Syntax

'Declaration
Public Property Stretch As Stretch
public Stretch Stretch { get; set; }
<ViewBox Stretch = "stretchValue"/>

XAML Values

  • stretchValue
    A named constant of the Stretch enumeration, such as Fill.

Property Value

Type: System.Windows.Media.Stretch
A Stretch mode, which determines how content fits in the available space. The default is Uniform.

Remarks

If you set the Stretch property to None, the content preserves its original size.

Stretch = None

ViewBox Stretch None

If you set the Stretch property to Uniform, the image is resized to fit the Viewbox. The Viewbox takes as much space as needed to show the entire image, while preserving the aspect ratio.

Stretch = Uniform

ViewBox Stretch Uniform

If you set the Stretch property to UniformToFill, the image is resized to fill the destination dimensions, while preserving its native aspect ratio. If the aspect ratio of the destination rectangle differs from the source, the source content is clipped to fit in the destination dimensions.

Stretch = UniformToFill

ViewBox Stretch UniformToFill

If you set the Stretch property to Fill, the image fills the entire space, but does not preserve the aspect ratio.

Stretch = Fill

ViewBox Stretch Fill

Examples

The following example shows how you can set the Stretch properties in C# and Visual Basic. This example is a part of a larger example available in the Viewbox class overview.

'Setting the Stretch property to None
Private Sub stretchNone(ByVal sender As Object, ByVal e As RoutedEventArgs)
    vb1.Stretch = Stretch.None
    vb2.Stretch = Stretch.None
    vb3.Stretch = Stretch.None
    txt1.Text = "Stretch is now set to None."
End Sub

'Setting the Stretch property to Fill
Private Sub stretchFill(ByVal sender As Object, ByVal e As RoutedEventArgs)
    vb1.Stretch = Stretch.Fill
    vb2.Stretch = Stretch.Fill
    vb3.Stretch = Stretch.Fill
    txt1.Text = "Stretch is now set to Fill."
End Sub

'Setting the Stretch property to Uniform
Private Sub stretchUni(ByVal sender As Object, ByVal e As RoutedEventArgs)
    vb1.Stretch = Stretch.Uniform
    vb2.Stretch = Stretch.Uniform
    vb3.Stretch = Stretch.Uniform
    txt1.Text = "Stretch is now set to Uniform."
End Sub

'Setting the Stretch property to UniformToFill
Private Sub stretchUniFill(ByVal sender As Object, ByVal e As RoutedEventArgs)
    vb1.Stretch = Stretch.UniformToFill
    vb2.Stretch = Stretch.UniformToFill
    vb3.Stretch = Stretch.UniformToFill
    txt1.Text = "Stretch is now set to UniformToFill."
End Sub
//Setting the Stretch property to None
private void stretchNone(object sender, RoutedEventArgs e)
{
    vb1.Stretch = Stretch.None;
    vb2.Stretch = Stretch.None;
    vb3.Stretch = Stretch.None;
    txt1.Text = "Stretch is now set to None.";
}

//Setting the Stretch property to Fill
private void stretchFill(object sender, RoutedEventArgs e)
{
    vb1.Stretch = Stretch.Fill;
    vb2.Stretch = Stretch.Fill;
    vb3.Stretch = Stretch.Fill;
    txt1.Text = "Stretch is now set to Fill.";
}

//Setting the Stretch property to Uniform
private void stretchUni(object sender, RoutedEventArgs e)
{
    vb1.Stretch = Stretch.Uniform;
    vb2.Stretch = Stretch.Uniform;
    vb3.Stretch = Stretch.Uniform;
    txt1.Text = "Stretch is now set to Uniform.";
}

//Setting the Stretch property to UniformToFill
private void stretchUniFill(object sender, RoutedEventArgs e)
{
    vb1.Stretch = Stretch.UniformToFill;
    vb2.Stretch = Stretch.UniformToFill;
    vb3.Stretch = Stretch.UniformToFill;
    txt1.Text = "Stretch is now set to UniformToFill.";
}

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.