Get WPF window height / width

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 height / width returns NaN or 0.0

Can someone tell me a way to get it?

These 2 methods do not 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 work in full screen mode.

+9
source share
6 answers

You can get the width and height that should have been specified in the constructor after the InitializeComponent was run, they will not return NaN , then the actual height and width will have to wait until the window.

When WindowState == Normal you can do this with Width / Height after IntializeComponent() .

When WindowState == Maximized you can get a screen resolution for this with

 System.Windows.SystemParameters.PrimaryScreenHeight; System.Windows.SystemParameters.PrimaryScreenWidth; 
+9
source

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

 this.SizeChanged += OnWindowSizeChanged; 

2.) Use the SizeChangedEventArgs 'e' object 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 that this is a very common case, you CAN (you also cannot) to do some checks to make sure you have values โ€‹โ€‹of size 0.

I used this to dynamically resize the list window when changing the main window. In fact, all I wanted was this control height to change the same value as the window height, so its parent panel looks consistent in all window changes.

Here is the code for this, a more specific example:

NOTE. I have a private instance instance called "resizeMode" that is set to 0 in the window code constructor 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; } 
+6
source

You need to try to get the ActualWidth/ActualHeight values โ€‹โ€‹as soon as the window loads in the user interface. Doing this in Window_Loaded works well.

+5
source

Xaml

 <Grid x:Name="Grid1"> <Image x:Name="Back_Image" HorizontalAlignment="Left" VerticalAlignment="Top"/> </Grid> 

CS MainWindow () after InitializeComponent ();

  Back_Image.Width = Grid1.Width; Back_Image.Height = Grid1.Height; 
+3
source

WPF makes creating controls and windows a delayed way. Therefore, until the window is displayed for the first time, it may not have passed the layout yet, so there was no ActualWidth / ActualHeight . You can wait until the window is loaded, and then get the properties, or better yet, bind these properties to the purpose for which you need them. You can also force the layout through UpdateLayout() .

I just want to add: try to minimize the size of the size-dependent logic, you can almost always avoid this. Unless, of course, you are writing a layout panel.

+1
source

Note that when you use controls with 100% of them, they have a NaN size until they are presented

You can check ActualHeigh or ActualWidth only when the Loaded event is fired, but never try to check it before creating Windows controls. Forget about controlling this in the constructor.

In my oppinion, the best place to manage such things is the SizeChanged event

+1
source

Source: https://habr.com/ru/post/917958/


All Articles