Search code examples
wpfxamlviewbox

Adjusting aspect ratio of my wpf window using Viewbox


I want to use viewbox for maintaining my wpf window's aspect ratio. i.e if i put my application on large monitor , it sholud automatically fit to that screen and if run it on my laptop it should adjust to its screen. Please help me out on this . it should be done purely on xaml wpf using view box.


Solution

  • Whats the problem with viewbox ? Just wrap viewbox around you root views Stretch property to one you like. If you want different scaling you need to write your own implementation of viewbox btw.

    Example:

    <Window x:Class="stackoverflowviewbox.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">
    <Viewbox Stretch="Uniform">
        <Grid>
            <Label>
                Hello world.Hello world.Hello world.Hello world.Hello world.Hello world.
            </Label>
        </Grid>
    </Viewbox>
    

    But it is possible to write xaml so views will look nice on different resolutions without viewbox without any problems.