Restrict Window.SizeToContent to extension only

Is it possible to limit Window.SizeToContentto expand the size?

I have a common window that is dynamically populated with content in TabControl . Since at first it is not known how large each TabItem is, I cannot pre-calculate the required size (I only care about the height) to display it all without scrolling.

The inclusion SizeToContentwill automatically resize to the required size whenever the tab is resized. Although it’s normal that it expands only when the current view needs more space, I don’t like that the window will also shrink when less space is needed. Is it possible to limit the behavior by SizeToContentsimply expanding the window size? Or is it possible to emulate the behavior in a different way, while maintaining the correct results, taking into account the window frames and other components next to the tab control?

I tried to connect to different events of the window to find out where the information of the new size goes when the tab is changed, but the only real useful in the window OnChildDesiredSizeChanged, did not produce deterministic results (for some tabs it was called, but for others it was not). Do you have another idea?

+5
source share
3 answers

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

public void WindowLoaded()
{
    this.MinHeight = this.ActualHeight;
    this.MinWidth = this.ActualWidth;
}
+3
source

I tried Bobs approach. His answer is the right approach.

In my case, the WindowLoaded event was fired earlier. It started before the content was displayed.

ContentRendered, , . , , "SizeChanged".

+3

In my case, I used the anchor ActualWidthand ActualHeightto MinWidthand MinHeightin the window:

    x:Name="printWnd"
    SizeToContent="WidthAndHeight"
    MinWidth="{Binding ActualWidth, ElementName=printWnd}"
    MinHeight="{Binding ActualHeight, ElementName=printWnd}"
0
source

All Articles