Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a WPF window's ClientSize?

Tags:

wpf

size

In WinForms, Form had a ClientSize property (inherited from Control), which returns the size of its client area, i.e., the area inside the title bar and window borders.

I'm not seeing anything similar in WPF: there's no ClientSize, ClientWidth, ClientHeight, GetClientSize(), or anything else that I can think to guess the name of.

How do I go about getting the client size of a WPF Window?

like image 934
Joe White Avatar asked Jun 05 '09 12:06

Joe White


People also ask

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

How do I center a window in WPF?

To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed. Run the program and click the button to see the result.

How do I know if a WPF window is open?

Windows. OfType<T>(). Any()) // Check is Not Open, Open it.

What is the difference between WPF window and WPF page?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.


1 Answers

One way you could do it is to take the top most child element, cast this.Content to its type, and call .RenderSize on it, which will give you its size.

<Window x:Class="XML_Reader.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="400" Width="600" WindowStyle="SingleBorderWindow">
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
    </Grid>
</Window>

((Grid)this.Content).RenderSize.Height
((Grid)this.Content).RenderSize.Width

edit:

as Trent said, ActualWidth and ActualHeight are also viable solutions. Basically easier methods of getting what I put above.

like image 186
CodeMonkey1313 Avatar answered Oct 13 '22 22:10

CodeMonkey1313