tags:

views:

29

answers:

2

I am using a Popup in the control template of a MenuItem.

However, I don't know in advance how many items will be in the menu item. As a result, the popup might not fit on the screen and some items might not be accessible.

My solution is to set MaxSize to, say, 500, and wrap the ItemsPresenter in a ScrollViewer.

However, this does not take into account the size of the screen.

Is there a way to specify that the popup should be as large as it needs to be without going outside the screen boundaries?

Apparently, WPF is smart enough to set the direction of the popup (up /down, right/ left) so that it tends to be centered. For example, if the popup is at the bottom of the screen, it will popup above the popup position, not below it. But I don't see anything regarding the max size.

+1  A: 

You could use a combination of SystemParameters.PrimaryScreenHeight and the top-left point of the Popup to calculate how much room you have, and set that as the MaxHeight of the Popup's contents.

HTH,
Kent

Kent Boogaart
Thanks for the answer. However, this does not work because the popup might popup anywhere on the screen.
Fair enough - updated my answer.
Kent Boogaart
Yes, that would do it. I was hoping there would be something easier (built in)
A: 

I believe that the only way you can do it is by hooking up to an event, perhaps the LayoutUpdated on the window or popup, and programmatically setting the width using some sort of percentage calculation. MaxWidth on FrameworkElements only takes discrete values.

Dave White