How to Scale WPF application according to screen resolution?

MERUN KUMAR MAITY 531 Reputation points
2021-01-31T03:54:25.177+00:00

I make a WPF application in .Net Framework 4.7. I developed this application on my Laptop which has full HD resolution (1920*1080). My problem is when I run my application on a low resolution monitor (1366*768), my application don't scale up according to the screen resolution. I tried using Per monitor DPI and do some edit in my App.Manifest but still the problem.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,706 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
786 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-02-01T02:26:05.063+00:00

    You can set Width and Height of the window according to the device's dispaly resolution, you can set it like below:

        <Window x:Class="WPFScale.MainWindow"  
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
                xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
                xmlns:local="clr-namespace:WPFScale"  
                mc:Ignorable="d"  
                Width="{x:Static SystemParameters.PrimaryScreenWidth}"  
                Height="{x:Static SystemParameters.PrimaryScreenHeight}"  
                Title="MainWindow">  
            <Grid>  
                 
            </Grid>  
        </Window>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Petrus 【KIM】 456 Reputation points
    2021-02-01T01:46:01.117+00:00
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                int nWidth = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
                int nHieght = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
    
                this.LayoutTransform = new ScaleTransform(nWidth / 1920, nHieght / 1080);
            }