methuselah
methuselah

Reputation: 13216

Scaling the elements within a WPF application across different screen sizes

I am trying to create an full-screen WPF application that is scales across a number of screen sizes. However, what I've found is that the window elements do not scale and position themselves correctly when the program in different screen sizes.

i.e. in VS2013

enter image description here

i.e. when run

enter image description here

How would I get the program to scale accordingly? For instance, I would like the buttons to be positioned to the either the left or right of the screen and the listbox to scale up to fill up the screen.

This is my code so far:

<Window x:Class="FYP.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <Button Content="View Messages" Grid.Column="1" HorizontalAlignment="Left" Margin="203,20,0,0" VerticalAlignment="Top" Width="97" Name="ViewMessagesButton"/>
        <Button Content="Log Out" Grid.Column="1" HorizontalAlignment="Left" Margin="305,20,0,0" VerticalAlignment="Top" Width="65" Name="LogOutButton"/>
        <TextBlock HorizontalAlignment="Left" Margin="20,20,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="20" Name="DateTimeTextBlock"/>
        <ListBox HorizontalAlignment="Left" Height="434" Margin="20,60,0,0" VerticalAlignment="Top" Width="350"/>
        <ListBox HorizontalAlignment="Left" Height="135" Margin="20,60,0,0" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
        <ListBox HorizontalAlignment="Left" Height="278" Margin="20,216,0,0" VerticalAlignment="Top" Width="350" Grid.Column="1"/>
    </Grid>
</Window>

Upvotes: 1

Views: 1269

Answers (3)

Murven
Murven

Reputation: 2387

If you really want the objects to scale up or down (as opposed to resizing), and you want the aspect ratio of your arrangement to remain the way you arranged it initially, you should be using WPF's ViewBox control.

Upvotes: 1

kidshaw
kidshaw

Reputation: 3451

Remove the width and heights, and set the horizontal and vertical alignments to stretch.

Upvotes: 1

Phoenix
Phoenix

Reputation: 913

Your problem is that you have set concrete sizes to all of you controls. Remove the width and height on your controls and set the margin the way you want.

Upvotes: 1

Related Questions