How can I get a WPF Window to expand to fit the contents of a dynamically changing ItemsControl?
Asked Answered
E

2

28

I've got a WPF application which starts off with a very small window. Something in the area of 200x100. When certain events are raised from an external source, I add a control to the ItemsControl area of my window. The items control is currently set to use a StackPanel PanelTemplate.

What I want, is for the application window to grow when controls are added to the itemscontrol. Is this at all possible? The idea behind this is to keep the window as small as possible at all times. It should also shrink when controls are removed from the itemscontrol.

Thanks.

Emendate answered 5/7, 2010 at 3:46 Comment(0)
T
54

Yes, it is possible. You can use Window.SizeToContent property to do that. And just set a MaxHeight and MaxWidth on Window or the ItemsControl inside just so it won't keep growing and getting out of screen bounds.

Tilefish answered 5/7, 2010 at 4:43 Comment(1)
I actually discovered the property right after asking the question. WPF is fairly overwhelming, I'm finding I skip past the easier things frequently. Thanks.Emendate
O
18

And if you would want to set it in XAML for WPF you could use

<Window
    SizeToContent="WidthAndHeight"
>

It can be separately set for the Height and the Width if required.

Obedience answered 17/2, 2015 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.