Alex Rivera | Logout

Get ListBox to resize with window, but not resize with content

Asked 2012-02-08T19:53:17.553
15

There must be an elegant solution to this problem, but I can't find anything online. I have a grid that has one column and row with width/height *, containing a ListBox. I have my Window SizeToContents set to WidthAndHeight to allow the window to resize to the proper size for each set of UI widgets/fonts. When I add items to the ListBox, it resizes, causing the window to grow.

I want the ListBox to resize if I change the size of the window, but if I add content that is longer than the width of the ListBox, I want the scrollbar to appear and not for it to grow, causing the Window to grow. If I set explicit sizes for the Window and set SizeToContent to Manual (the default), it works as I intend.

Is there any way to size the window to the contents at startup and continue to have the ListBox grow with the window size, but not with its content?

Edit
Report

1 Answer

1

This is the intended behavior resulted from setting the SizeToContent property to WidthAndHeight as described here.

What you might do is binding the Width and the Height of the ListBox to the ActualWidth and ActualHeight properties of its container, or use a converter and bind them directly to the Window's respective properties.

answered 2013-03-20T11:44:00.213

Your Answer