Class ViewportOverlayPositioner
Provides unified positioning for overlay elements that need to appear at specific viewport coordinates, regardless of the parent container type.
Problem this solves: FlowGraph has two rendering modes with different container types:
- Retained Mode: Uses MainCanvas (Canvas) with MatrixTransform - Canvas.SetLeft/SetTop work
- Direct Rendering: Uses RootPanel (Panel) - Canvas.SetLeft/SetTop are IGNORED
Key insight: Canvas attached properties (Canvas.Left, Canvas.Top) only work when the parent element is a Canvas. When attached to a Panel child, they have no effect.
public class ViewportOverlayPositioner
- Inheritance
-
ViewportOverlayPositioner
- Inherited Members
Examples
// Instead of this error-prone code:
if (isDirectMode)
{
// BUG: Won't work - RootPanel is Panel, not Canvas!
Canvas.SetLeft(element, x);
Canvas.SetTop(element, y);
}
// Use the positioner:
var positioner = new ViewportOverlayPositioner(isDirectMode);
positioner.SetPosition(element, viewportX, viewportY);
Constructors
ViewportOverlayPositioner(bool)
Creates a new viewport overlay positioner.
public ViewportOverlayPositioner(bool isDirectRenderingMode)
Parameters
isDirectRenderingModeboolTrue when using Direct Rendering (RootPanel/Panel parent). False when using Retained Mode (MainCanvas/Canvas parent).
Methods
Create(DirectCanvasRenderer?)
Creates a positioner based on whether a direct renderer is active.
public static ViewportOverlayPositioner Create(DirectCanvasRenderer? directRenderer)
Parameters
directRendererDirectCanvasRendererThe direct renderer, or null if in retained mode.
Returns
- ViewportOverlayPositioner
A positioner configured for the current rendering mode.
PrepareForPositioning(Control)
Sets up an element for proper positioning in the target container. Call this when creating elements that will be positioned later.
public void PrepareForPositioning(Control element)
Parameters
elementControlThe element to configure.
Remarks
This sets HorizontalAlignment and VerticalAlignment to Left/Top for Panel parents. For Canvas parents, these properties are ignored but don't cause harm.
SetPosition(Control, Point)
Positions an element at the specified Avalonia point (viewport coordinates).
public void SetPosition(Control element, Point position)
Parameters
elementControlThe element to position.
positionPointPosition in viewport space.
SetPosition(Control, ViewportPoint)
Positions an element at the specified viewport point.
public void SetPosition(Control element, ViewportPoint position)
Parameters
elementControlThe element to position.
positionViewportPointPosition in viewport space.
SetPosition(Control, double, double)
Positions an element at the specified viewport coordinates. Automatically uses the correct positioning method based on the rendering mode.
public void SetPosition(Control element, double x, double y)