/// <summary> /// Updates the location and size of the Popup and overlay. /// </summary> private void UpdateContextMenuPlacement() { if ((null != _rootVisual) && (null != _overlay)) { // Start with the current Popup alignment point double x = _popupAlignmentPoint.X; double y = _popupAlignmentPoint.Y; // Adjust for offset #if !WINDOWS_PHONE x += HorizontalOffset; #endif y += VerticalOffset; #if WINDOWS_PHONE // Determine frame/page bounds bool portrait = (_rootVisual.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait; double effectiveWidth = portrait ? _rootVisual.ActualWidth : _rootVisual.ActualHeight; double effectiveHeight = portrait ? _rootVisual.ActualHeight : _rootVisual.ActualWidth; Rect bounds = new Rect(0, 0, effectiveWidth, effectiveHeight); if (_page != null) { bounds = _page.TransformToVisual(_rootVisual).TransformBounds(new Rect(0, 0, _page.ActualWidth, _page.ActualHeight)); } // Left align with full width x = bounds.Left; Width = bounds.Width; // Ensure the bottom is visible / ensure the top is visible y = Math.Min(y, bounds.Bottom - ActualHeight); y = Math.Max(y, bounds.Top); #else // Try not to let it stick out too far to the right/bottom x = Math.Min(x, _rootVisual.ActualWidth - ActualWidth); y = Math.Min(y, _rootVisual.ActualHeight - ActualHeight); #endif // Do not let it stick out too far to the left/top x = Math.Max(x, 0); y = Math.Max(y, 0); // Set the new location Canvas.SetLeft(this, x); Canvas.SetTop(this, y); // Size the overlay to match the new container #if WINDOWS_PHONE _overlay.Width = effectiveWidth; _overlay.Height = effectiveHeight; #else _overlay.Width = _rootVisual.ActualWidth; _overlay.Height = _rootVisual.ActualHeight; #endif } }