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.
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.
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.
Note, images. A Viewbox makes it easier to replace the inner element in a dynamic way. If you nest an Image within a Viewbox, you can replace that image in C# code.
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 1, 2023 (simplify).