public static FrameworkElement GetPlacementParent(Windows.Popup popup) { //popup.AssertNotNull("popup"); return popup.GetValue(PlacementParentProperty) as FrameworkElement; }
public PopupWatcher(Windows.Popup popup) { this.popup = popup; }
public static void SetPlacementChild(Windows.Popup popup, FrameworkElement placementChild) { //popup.AssertNotNull("popup"); popup.SetValue(PlacementChildProperty, placementChild); }
private static void InvalidatePosition(Windows.Popup popup) { if (popup.IsOpen) PositionPopup(popup); popup.Opened -= OnPopupOpened; popup.Opened += OnPopupOpened; var child = GetPlacementChild(popup) ?? popup.Child as FrameworkElement; if (child != null) { SetPopup(child, popup); child.SizeChanged -= OnChildSizeChanged; child.SizeChanged += OnChildSizeChanged; } }
private static FrameworkElement FindHighestAncestor(Windows.Popup popup) { var ancestor = (FrameworkElement)popup; while (true) { var parent = VisualTreeHelper.GetParent(ancestor) as FrameworkElement; if (parent == null) return ancestor; ancestor = parent; } }
private static void PositionPopup(Windows.Popup popup) { var parent = popup.Parent as FrameworkElement; var placementParent = GetPlacementParent(popup) ?? popup.Parent as FrameworkElement; var placementChild = GetPlacementChild(popup) ?? popup.Child as FrameworkElement; if (parent == null || placementParent == null || placementChild == null) return; var preferredOrientations = GetPreferredOrientations(popup) ?? Enumerable.Empty<PopupOrientation>(); foreach (var preferredOrientation in preferredOrientations) { if (TryPlacePopup(popup, parent, placementParent, placementChild, preferredOrientation)) return; } var fallbackOrientations = from placement in popupPlacements from horizontalAlignment in horizontalAlignments from verticalAlignment in verticalAlignments where preferredOrientations.FirstOrDefault(x => x.Placement == placement && x.HorizontalAlignment == horizontalAlignment && x.VerticalAlignment == verticalAlignment) == null select new PopupOrientation() { Placement = placement, HorizontalAlignment = horizontalAlignment, VerticalAlignment = verticalAlignment }; foreach (var fallbackOrientation in fallbackOrientations) { if (TryPlacePopup(popup, parent, placementParent, placementChild, fallbackOrientation)) return; } // give up and just use the first preferred orientation, if any var orientation = GetPreferredOrientations(popup).FirstOrDefault(); if (orientation != null) SetActualOrientation(popup, orientation); }
private static bool TryPlacePopup(Windows.Popup popup, FrameworkElement parent, FrameworkElement placementParent, FrameworkElement placementChild, PopupOrientation orientation) { var availableWidth = placementParent.ActualWidth; var availableHeight = placementParent.ActualHeight; SetActualOrientation(popup, orientation); placementChild.Measure(new Size(availableWidth, availableHeight)); var requiredWidth = placementChild.DesiredSize.Width; var requiredHeight = placementChild.DesiredSize.Height; var placementTarget = GetPlacementTarget(popup) ?? parent; var parentTransform = placementTarget.TransformToVisual(parent); var popupLocation = parentTransform.Transform(new Point(0, 0)); switch (orientation.Placement) { case PopupPlacement.Top: popupLocation.Y -= requiredHeight; break; case PopupPlacement.Bottom: popupLocation.Y += placementTarget.ActualHeight; break; case PopupPlacement.Left: popupLocation.X -= requiredWidth; break; case PopupPlacement.Right: popupLocation.X += placementTarget.ActualWidth; break; } if (orientation.Placement == PopupPlacement.Top || orientation.Placement == PopupPlacement.Bottom) { switch (orientation.HorizontalAlignment) { case PopupHorizontalAlignment.RightCenter: popupLocation.X += ((placementTarget.ActualWidth / 2) - requiredWidth); break; case PopupHorizontalAlignment.Center: popupLocation.X += ((placementTarget.ActualWidth - requiredWidth) / 2); break; case PopupHorizontalAlignment.LeftCenter: popupLocation.X += (placementTarget.ActualWidth / 2); break; case PopupHorizontalAlignment.Right: popupLocation.X += (placementTarget.ActualWidth - requiredWidth); break; } } if (orientation.Placement == PopupPlacement.Left || orientation.Placement == PopupPlacement.Right) { switch (orientation.VerticalAlignment) { case PopupVerticalAlignment.BottomCenter: popupLocation.Y += ((placementTarget.ActualHeight / 2) - requiredHeight); break; case PopupVerticalAlignment.Center: popupLocation.Y += ((placementTarget.ActualHeight - requiredHeight) / 2); break; case PopupVerticalAlignment.TopCenter: popupLocation.Y += (placementTarget.ActualHeight / 2); break; case PopupVerticalAlignment.Bottom: popupLocation.Y += (placementTarget.ActualHeight - requiredHeight); break; } } var popupLocationRelativeToPlacementParent = popupLocation; if (parent != placementParent) { var placementParentTransform = placementTarget.TransformToVisual(placementParent); popupLocationRelativeToPlacementParent = placementParentTransform.Transform(popupLocation); } if (popupLocationRelativeToPlacementParent.X < 0 || popupLocationRelativeToPlacementParent.Y < 0 || (popupLocationRelativeToPlacementParent.X + requiredWidth) > availableWidth || (popupLocationRelativeToPlacementParent.Y + requiredHeight) > availableHeight) { // not enough room return false; } popup.HorizontalOffset = popupLocation.X; popup.VerticalOffset = popupLocation.Y; return true; }
public static void SetStaysOpen(Windows.Popup popup, bool staysOpen) { //popup.AssertNotNull("popup"); popup.SetValue(StaysOpenProperty, staysOpen); }
private static void SetPopup(DependencyObject dependencyObject, Windows.Popup popup) { //popup.AssertNotNull("popup"); dependencyObject.SetValue(PopupProperty, popup); }
public static bool GetStaysOpen(Windows.Popup popup) { //popup.AssertNotNull("popup"); return (bool)popup.GetValue(StaysOpenProperty); }
public static void SetActualOrientation(Windows.Popup popup, PopupOrientation orientation) { //popup.AssertNotNull("popup"); popup.SetValue(ActualOrientationProperty, orientation); }
public static PopupOrientation GetActualOrientation(Windows.Popup popup) { //popup.AssertNotNull("popup"); return popup.GetValue(ActualOrientationProperty) as PopupOrientation; }
public static void SetPreferredOrientations(Windows.Popup popup, PopupOrientationCollection orientations) { //popup.AssertNotNull("popup"); popup.SetValue(PreferredOrientationsProperty, orientations); }
public static PopupOrientationCollection GetPreferredOrientations(Windows.Popup popup) { //popup.AssertNotNull("popup"); return popup.GetValue(PreferredOrientationsProperty) as PopupOrientationCollection; }