Home
WPF
Viewbox Example
Updated Dec 24, 2024
Dot Net Perls
Viewbox, WPF. A Viewbox resizes the control nested within it. With its Stretch attribute, we resize the element to fill the entire Viewbox.
Getting started. Please drag a Viewbox to your Window—it will be nested within the default Grid. Now adjust its properties. Add a Stretch attribute.
Image
Example. Here we adjust a Viewbox and its XAML. In the screenshot, the Stretch attribute is set to "Fill." So the text in the TextBox becomes distorted.
Tip Using "Fill" will not look good: it may also lead to blurring or other pixel artifacts.
TextBox
Tip 2 When using a Viewbox, I recommend trying all the Stretch options to find the best one for your purpose.
<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Viewbox HorizontalAlignment="Left" Height="299"
                 Margin="10,10,0,0" VerticalAlignment="Top" Width="497"
                 Stretch="Fill">
            <TextBox Height="23" TextWrapping="Wrap" Text="TextBox" Width="120"/>
        </Viewbox>
    </Grid>
</Window>
RenderTransform. A Viewbox is not the only way to scale and transform a control. You can add a RenderTransform element inside your control to specify how it is rendered.
Tip RenderTransform element can apply skew, scale, rotate and translate transformations.
RenderTransform
Note, simple effects. The Viewbox is great for simple stuff. A DoubleAnimation element (along with a Storyboard) can be used for animating things.
DoubleAnimation
WPF has strong support for transforming how elements, including controls, pictures and text, are rendered. And Viewbox is another way we exploit these capabilities.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (simplify).
Home
Changes
© 2007-2025 Sam Allen