Get the height/width of Window WPF

Total Post:102

Points:714

WPF 
WPF
 9433  View(s)
Ratings:
Rate this:

I have the following code

<Window x:Class="Netspot.DigitalSignage.Client.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" WindowStyle="SingleBorderWindow"
        WindowStartupLocation="CenterScreen"
        WindowState="Normal" Closing="Window_Closing">

Any attempt to get the height / width return NaN or 0.0

Can anyone tell me a way of getting it ?

These 2 methods don't work

//Method1

var h = ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualHeight;
var w = ((System.Windows.Controls.Panel)Application.Current.MainWindow.Content).ActualWidth;

//Method2

double dWidth = -1;
double dHeight = -1;
FrameworkElement pnlClient = this.Content as FrameworkElement;
if (pnlClient != null)
{
     dWidth = pnlClient.ActualWidth;
     dHeight = pnlClient.ActualWidth;
}

The application will not be running full screen.

  1. Re: Get the height/width of Window WPF

    Hi Mark,

    You can get the width and height that the window was meant to be in the constructor after InitializeComponent has been run, they won't return NaN then, the actual height and width will have to wait until the window has been displayed.

    When WindowState == Normal You can do this one from Width /
    Height after IntializeComponent().
    When WindowState == Maximized You could get the screen resolution for this one with
    System.Windows.SystemParameters.PrimaryScreenHeight;
    System.Windows.SystemParameters.PrimaryScreenWidth;

      Modified On Apr-04-2018 03:36:11 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.