开发者

How can I restrict the size of a popup to the screen

开发者 https://www.devze.com 2023-01-22 10:16 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消