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 开发者_JS百科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.
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.
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.
精彩评论