Search code examples
wpfsize

How to get a WPF window's ClientSize?


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?


Solution

  • 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.