Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Making child elements' MinWidth/MinHeight constrain the Window

Tags:

layout

wpf

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?

like image 688
Joe White Avatar asked Mar 08 '11 04:03

Joe White


1 Answers

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

like image 77
Joe White Avatar answered Nov 08 '22 16:11

Joe White