Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain screen size from xaml?

Tags:

c#

wpf

I'm using wpf on C# to design GUI, and I want to get screen size (The value of Width and Height) from xaml code.

I knew how to get them from C# code as

   Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
   Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

But I don't know how to get them from XAML code.

like image 687
TTGroup Avatar asked Jul 02 '13 02:07

TTGroup


People also ask

What method is used to get the size of a browser window?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.

What is XAML in C#?

In a Xamarin. Forms application, XAML is mostly used to define the visual contents of a page and works together with a C# code-behind file. The code-behind file provides code support for the markup. Together, these two files contribute to a new class definition that includes child views and property initialization.

What is our XAML?

Extensible Application Markup Language (XAML) is a declarative language that's based on XML. XAML is used extensively in the following types of applications to build user interfaces: Windows Presentation Foundation (WPF) apps. Universal Windows Platform (UWP) apps.


1 Answers

This will work. You can read more here about SystemParameters

<Window x:Class="WpfApplication1.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">
    <StackPanel>
        <TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenHeight}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.FullPrimaryScreenWidth}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenHeight}}" />
        <TextBlock Text="{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}}" />
    </StackPanel>
</Window>
like image 197
Vlad Bezden Avatar answered Nov 01 '22 12:11

Vlad Bezden