Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 278851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T01:18:17+00:00 2026-05-12T01:18:17+00:00

I want to set my WPF window’s initial client size. I’m not seeing a

  • 0

I want to set my WPF window’s initial client size. I’m not seeing a straightforward way to do this.

Specifically, when my window opens, I want it to be sized just big enough for its contents to fit without needing scrollbars. But after it’s shown, I want the window to be freely resizable (either larger or smaller).

If I set Width and Height attributes on my Window element, that sets the non-client (outer) size, which isn’t useful. Once the titlebar and resize borders eat into that space, the client area will no longer be big enough for its content, and I’ll have scrollbars. I could compensate by picking a larger size, but both titlebar height and border thickness are user-customizable (as well as the defaults varying by OS version) and won’t necessarily be the same on a different machine.

I can set Width and Height on the window’s content element (a <Grid> in this case), and then set the Window’s SizeToContent attribute to WidthAndHeight. That gets the window’s initial size exactly where I want it. But then things don’t resize anymore — I can resize the window, but its content doesn’t resize with it, because I specified a fixed size.

Is there any way to set a Window’s initial client size, preferably without code-behind? (I’ll take code-behind if that’s the only way, but I’d prefer a XAML-only approach if anyone has one.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T01:18:17+00:00Added an answer on May 12, 2026 at 1:18 am

    You can do it in code-behind on the Load event handler in one of two ways:

    NOTE: The content of the LayoutRoot Grid is the same in both examples, but the Width and Height on the LayoutRoot are only specified in example A.

    A) ClearValue on the the Window’s SizeToContent and on the content’s Width and Height:

    using System.Windows;
    
    namespace WpfWindowBorderTest
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                ClearValue(SizeToContentProperty);
                LayoutRoot.ClearValue(WidthProperty);
                LayoutRoot.ClearValue(HeightProperty);
            }
        }
    }
    

    assuming a page layout like (note the SizeToContent setting and Loaded event handler on the Window and the Width and Height specified on the LayoutRoot):

    <Window x:Class="WpfWindowBorderTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" SizeToContent="WidthAndHeight" Loaded="Window_Loaded">
        <Grid x:Name="LayoutRoot" Width="300" Height="300" Background="Green">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="3*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" />
            <Rectangle Grid.Row="0" Grid.Column="0" Width="75" Height="75" Fill="Red" />
            <Rectangle Grid.Row="1" Grid.Column="1" Fill="Yellow" />
            <Rectangle Grid.Row="1" Grid.Column="1" Width="225" Height="225" Fill="Red" />
        </Grid>
    </Window>
    

    or

    B) setting the Window’s Width and Height accounting for the System-specific client window frame sizes:

    using System.Windows;

    namespace WpfWindowBorderTest
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                const int snugContentWidth = 300;
                const int snugContentHeight = 300;
    
                var horizontalBorderHeight = SystemParameters.ResizeFrameHorizontalBorderHeight;
                var verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
                var captionHeight = SystemParameters.CaptionHeight;
    
                Width = snugContentWidth + 2 * verticalBorderWidth;
                Height = snugContentHeight + captionHeight + 2 * horizontalBorderHeight;
            }
        }
    }
    

    assuming a proportional page layout like (note no SizeToContent setting or Loaded event handler on the Window or Width and Height specified on the LayoutRoot):

    <Window x:Class="WpfWindowBorderTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1">
        <Grid x:Name="LayoutRoot" Background="Green">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="3*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" />
            <Rectangle Grid.Row="0" Grid.Column="0" Width="75" Height="75" Fill="Red" />
            <Rectangle Grid.Row="1" Grid.Column="1" Fill="Yellow" />
            <Rectangle Grid.Row="1" Grid.Column="1" Width="225" Height="225" Fill="Red" />
        </Grid>
    </Window>
    

    I haven’t been able to come up with a way to do it declaratively in XAML as yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want a WPF window to fade in when in opens. I set the
I've got WPF window(WPF browser app) with 3 rectangles. I want to set trigger
I want in the window of a wpf to set a treeview and make
I have a WPF Window with WindowStyle set to none. Is there some way
I know that in XAML/WPF, I can set the Window size using the Width
Using WPF, I have a TreeView control that I want to set its ItemTemplate
I want set Listbox background to transparent but not working Is there any idea?
I want to set a background image for a div, in a way that
I have a simple WPF window that has 12 buttons on it. I want
This one has me beat; I have a WPF window with two (important for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.