Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the height/width of Window WPF

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.

like image 390
Welsh King Avatar asked Jun 13 '12 10:06

Welsh King


People also ask

How to get Window Size in WPF?

If you have any child element in your window, and the element is fill docked in your window, you can get the ActualWidth and ActualHeight of the element, they are same as the window client size. You should get ActualWidth and ActualHeight after the Load event of the element.

What is Width and Height in WPF?

The Height and the Width of window is 300 and 400 pixels. The type of Width and Height is a double device-independent unit (1/96th inch). This value can be followed by strings px, in, cm, or pt that a pixel, inch, centimeter, or point respectively.

How do I change the window size in Winui 3?

appWindow. Resize(new Windows. Graphics. SizeInt32 { Width = 480, Height = 800 });


2 Answers

1.) Subscribe to the window re size event in the code behind:

this.SizeChanged += OnWindowSizeChanged;

2.) Use the SizeChangedEventArgs object 'e' to get the sizes you need:

protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
{
    double newWindowHeight = e.NewSize.Height;
    double newWindowWidth = e.NewSize.Width;
    double prevWindowHeight = e.PreviousSize.Height;
    double prevWindowWidth = e.PreviousSize.Width;
}

Keep in mind this is very general case, you MAY (you may not either) have to do some checking to make sure you have size values of 0.

I used this to resize a list box dynamically as the main window changes. Essentially all I wanted was this control's height to change the same amount the window's height changes so its parent 'panel' looks consistent throughout any window changes.

Here is the code for that, more specific example:

NOTE I have a private instance integer called 'resizeMode' that is set to 0 in the constructor the Window code behind.

Here is the OnWindowSizeChanged event handler:

    protected void OnWindowSizeChanged (object sender, SizeChangedEventArgs e)
    {
        if (e.PreviousSize.Height != 0)
        {
            if (e.HeightChanged)
            {
                double heightChange = e.NewSize.Height - e.PreviousSize.Height;
                if (lbxUninspectedPrints.Height + heightChange > 0)
                {
                    lbxUninspectedPrints.Height = lbxUninspectedPrints.Height + heightChange;
                }
            }
        }
        prevHeight = e.PreviousSize.Height;
    }
like image 70
kformeck Avatar answered Sep 20 '22 15:09

kformeck


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;
like image 40
Andy Avatar answered Sep 20 '22 15:09

Andy