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 4061190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:26:07+00:00 2026-05-20T15:26:07+00:00

I have a WPF Window that contains a UserControl with a MinWidth and MinHeight.

  • 0

I have a WPF Window that contains a UserControl with a MinWidth and MinHeight. How can I prevent the user from resizing the Window down to a point where that UserControl’s minimum size is violated?

Here’s a simplified version of a Window I’m working on. My real app’s UserControl is replaced here by a Border:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <DockPanel>
    <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
      <Button Content="OK"/>
      <Button Content="Cancel"/>
    </StackPanel>
    <Border BorderBrush="Green" BorderThickness="10"
            MinWidth="200" MinHeight="150"/>
  </DockPanel>
</Window>

If I shrink the window small enough, the Border’s right and bottom edges are cut off. I want to prevent the window from getting that small — I want my window’s minimum size to be exactly the point at which the Border is at its minimum size. Some frameworks (like the Delphi VCL) automatically aggregate child controls’ minimum sizes up to the window; I expected WPF to do the same, but clearly it does not.

I could always explicitly set the Window’s MinWidth and MinHeight, but to calculate those correctly, I would have to factor in the Buttons’ ActualHeight, which would mean waiting for at least one layout pass (or calling Measure manually). Messy.

Is there any better way to keep the Window from resizing too small for its content?

  • 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-20T15:26:08+00:00Added an answer on May 20, 2026 at 3:26 pm

    The simplest way I’ve found is to tell the Window to size to its content:

    <Window ... SizeToContent="WidthAndHeight" ...>
    

    and then, once it’s done sizing (which will take the child elements’ MinWidth and MinHeight into account), run some code that sets MinWidth and MinHeight to the window’s ActualWidth and ActualHeight. It’s also a good idea to turn SizeToContent back off at this point, lest the window resize when its content changes.

    The next question is, where to put this code? I finally settled on OnSourceInitialized:

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        MinWidth = ActualWidth;
        MinHeight = ActualHeight;
        ClearValue(SizeToContentProperty);
    }
    

    I also tried the Loaded event, but in my case, that was too soon — Loaded happens before databindings have been evaluated, and I had databindings that affected my layout (a Label with a binding for its Content — its size changed after the binding took effect). Moving the code into OnSourceInitialized, which fires after databinding, corrected the problem.

    (There were also other events that fired after binding, but before the window was shown — SizeChanged and LayoutUpdated — but they both fire multiple times as the window is shown, and again later if the user resizes the window; OnSourceInitialized only fires once, which I consider ideal.)

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

Sidebar

Related Questions

I have a WPF application that contains some Window s with few UserControl s
I have a WPF app with a usercontrol that contains a HwndHost. The HwndHost
I have a WPF window with a TreeView that contains a checkbox at each
WPF doesn't provide the ability to have a window that allows resize but doesn't
I have a window in WPF which shows some media contents. This content contains
I have a WPF application that is made up of a window containing a
I have a WPF window that has TopMost=true . When I call another Window
I have an WPF usercontrol that has a TextBox. I set the text Underline
I have built a Wpf UserControl Library and it contains a large group of
I Have a WPF window that have a RadGridView and a Button, My requirement

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.