Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict Window.SizeToContent to expand only

Tags:

window

wpf

Is it somehow possible to restrict Window.SizeToContent to expand the size only?

I have a general window that is dynamically filled with content in a TabControl. As it is not known at the beginning how large each TabItem is going to be I cannot precalculate the required size (I only care about height though) to display it all without scrolling.

Enabling SizeToContent will automatically adjust the size to the required size whenever the tab is changed. While it’s okay that it only expands when the current view requires more space, I dislike that the window will also shrink when less space is required. Is it possible to restrict the SizeToContent behavior to just allow expansion of the window size? Or can the behavior be emulated in a different way, while still producing correct results taking the window frames and other components next to the tab control into account?

I tried hooking into different window events to find out where the new size information goes when changing the tab, but the only real useful one on the window, OnChildDesiredSizeChanged, was not producing deterministic results (for some tabs it was called, for others it wasn’t). Do you have any other idea?

like image 753
poke Avatar asked Mar 05 '13 14:03

poke


1 Answers

You can try using FrameworkElement.MinHeight and FrameworkElement.MinWidth to set the minimum height/width after the Window has loaded.

public void WindowLoaded()
{
    this.MinHeight = this.ActualHeight;
    this.MinWidth = this.ActualWidth;
}
like image 68
Bob. Avatar answered Sep 28 '22 09:09

Bob.