Skip to content

Commit cfbad79

Browse files
fix: fix popups attached to UIElements's position when inside another Window than the MainWindow
1 parent 41d648c commit cfbad79

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Runtime/Runtime/Core/Rendering/INTERNAL_PopupsManager.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,16 @@ public static Point CalculatePopupAbsolutePositionBasedOnElementPosition(UIEleme
129129
}
130130
}
131131

132+
/// <summary>
133+
/// Returns the coordinates of the UIElement, relative to the Window that contains it.
134+
/// </summary>
135+
/// <param name="element">The element of which the position will be returned.</param>
136+
/// <returns>The position of the element relative to the Window that contains it.</returns>
132137
public static Point GetUIElementAbsolutePosition(UIElement element)
133138
{
134139
if (INTERNAL_VisualTreeManager.IsElementInVisualTree(element))
135140
{
136-
GeneralTransform gt = element.TransformToVisual(null);
137-
138-
// Note: by passing "null" to "TransformToVisual", we tell the framework to
139-
// get the coordinates relative to the Window root.
140-
141+
GeneralTransform gt = element.TransformToVisual(Window.GetWindow(element));
141142
return gt.Transform(new Point(0d, 0d));
142143
}
143144
return new Point();

src/Runtime/Runtime/System.Windows.Controls.Primitives/Popup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ private void UpdatePosition()
447447
try
448448
{
449449
targetBounds = target
450-
.TransformToVisual(null)
450+
.TransformToVisual(Window.GetWindow(target))
451451
.TransformBounds(new Rect(0, 0, target.ActualWidth, target.ActualHeight));
452452
}
453453
catch { }

src/Runtime/Runtime/System.Windows/Window.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,14 @@ protected void OnClosing(ClosingEventArgs eventArgs)
401401
[OpenSilver.NotImplemented]
402402
public static Window GetWindow(DependencyObject dependencyObject)
403403
{
404+
//TODO: this should in theory throw an InvaliOperationException if the dependencyObject is not valid but I didn't check what made it not valid (it looks like Windows themselves are not valid).
405+
// In Silverlight, doing: Window.GetWindow(new Border()); returns the main window if it was made from the main window, null if from another window.
406+
407+
UIElement dependencyObjectAsUIElement = dependencyObject as UIElement;
408+
if (dependencyObjectAsUIElement != null)
409+
{
410+
return dependencyObjectAsUIElement.INTERNAL_ParentWindow;
411+
}
404412
return null;
405413
}
406414

0 commit comments

Comments
 (0)