Table of Contents

Class Visual

Namespace
Avalonia
Assembly
Avalonia.Base.dll

Base class for controls that provides rendering and related visual properties.

[UsableDuringInitialization]
public class Visual : StyledElement, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed
Inheritance
Visual
Implements
Derived
Inherited Members
Extension Methods

Remarks

The Visual class represents elements that have a visual on-screen representation and stores all the information needed for an IRenderer to render the control. To traverse the visual tree, use the extension methods defined in VisualExtensions.

Constructors

Visual()

Initializes a new instance of the Visual class.

public Visual()

Fields

BoundsProperty

Defines the Bounds property.

public static readonly DirectProperty<Visual, Rect> BoundsProperty

Field Value

DirectProperty<Visual, Rect>

ClipProperty

Defines the Clip property.

public static readonly StyledProperty<Geometry?> ClipProperty

Field Value

StyledProperty<Geometry>

ClipToBoundsProperty

Defines the ClipToBounds property.

public static readonly StyledProperty<bool> ClipToBoundsProperty

Field Value

StyledProperty<bool>

EffectProperty

Defines the Effect property.

public static readonly StyledProperty<IEffect?> EffectProperty

Field Value

StyledProperty<IEffect>

FlowDirectionProperty

Defines the FlowDirection property.

public static readonly AttachedProperty<FlowDirection> FlowDirectionProperty

Field Value

AttachedProperty<FlowDirection>

HasMirrorTransformProperty

Defines the HasMirrorTransform property.

public static readonly DirectProperty<Visual, bool> HasMirrorTransformProperty

Field Value

DirectProperty<Visual, bool>

IsVisibleProperty

Defines the IsVisible property.

public static readonly StyledProperty<bool> IsVisibleProperty

Field Value

StyledProperty<bool>

OpacityMaskProperty

Defines the OpacityMask property.

public static readonly StyledProperty<IBrush?> OpacityMaskProperty

Field Value

StyledProperty<IBrush>

OpacityProperty

Defines the Opacity property.

public static readonly StyledProperty<double> OpacityProperty

Field Value

StyledProperty<double>

RenderTransformOriginProperty

Defines the RenderTransformOrigin property.

public static readonly StyledProperty<RelativePoint> RenderTransformOriginProperty

Field Value

StyledProperty<RelativePoint>

RenderTransformProperty

Defines the RenderTransform property.

public static readonly StyledProperty<ITransform?> RenderTransformProperty

Field Value

StyledProperty<ITransform>

VisualParentProperty

Defines the Avalonia.Visual.VisualParent property.

public static readonly DirectProperty<Visual, Visual?> VisualParentProperty

Field Value

DirectProperty<Visual, Visual>

ZIndexProperty

Defines the ZIndex property.

public static readonly StyledProperty<int> ZIndexProperty

Field Value

StyledProperty<int>

Properties

Bounds

Gets the bounds of the control relative to its parent.

public Rect Bounds { get; protected set; }

Property Value

Rect

BypassFlowDirectionPolicies

Gets a value indicating whether control bypass FlowDirecton policies.

protected virtual bool BypassFlowDirectionPolicies { get; }

Property Value

bool

Remarks

Related to FlowDirection system and returns false as default, so if FlowDirection is RTL then control will get a mirror presentation. For controls that want to avoid this behavior, override this property and return true.

Clip

Gets or sets the geometry clip for this visual.

public Geometry? Clip { get; set; }

Property Value

Geometry

ClipToBounds

Gets or sets a value indicating whether the control should be clipped to its bounds.

public bool ClipToBounds { get; set; }

Property Value

bool

Effect

Gets or sets the effect of the control.

public IEffect? Effect { get; set; }

Property Value

IEffect

FlowDirection

Gets or sets the text flow direction.

public FlowDirection FlowDirection { get; set; }

Property Value

FlowDirection

HasMirrorTransform

Gets or sets a value indicating whether to apply mirror transform on this control.

public bool HasMirrorTransform { get; protected set; }

Property Value

bool

IsEffectivelyVisible

Gets a value indicating whether this control and all its parents are visible.

public bool IsEffectivelyVisible { get; }

Property Value

bool

IsVisible

Gets or sets a value indicating whether this control is visible.

public bool IsVisible { get; set; }

Property Value

bool

Opacity

Gets or sets the opacity of the control.

public double Opacity { get; set; }

Property Value

double

OpacityMask

Gets or sets the opacity mask of the control.

public IBrush? OpacityMask { get; set; }

Property Value

IBrush

RenderTransform

Gets or sets the render transform of the control.

public ITransform? RenderTransform { get; set; }

Property Value

ITransform

RenderTransformOrigin

Gets or sets the transform origin of the control.

public RelativePoint RenderTransformOrigin { get; set; }

Property Value

RelativePoint

VisualChildren

Gets the control's child visuals.

protected IAvaloniaList<Visual> VisualChildren { get; }

Property Value

IAvaloniaList<Visual>

VisualRoot

Gets the root of the visual tree, if the control is attached to a visual tree.

protected IRenderRoot? VisualRoot { get; }

Property Value

IRenderRoot

ZIndex

Gets or sets the Z index of the control.

public int ZIndex { get; set; }

Property Value

int

Remarks

Controls with a higher ZIndex will appear in front of controls with a lower ZIndex. If two controls have the same ZIndex then the control that appears later in the containing element's children collection will appear on top.

Methods

AffectsRender<T>(params AvaloniaProperty[])

Indicates that a property change should cause InvalidateVisual() to be called.

protected static void AffectsRender<T>(params AvaloniaProperty[] properties) where T : Visual

Parameters

properties AvaloniaProperty[]

The properties.

Type Parameters

T

The control which the property affects.

Remarks

This method should be called in a control's static constructor with each property on the control which when changed should cause a redraw. This is similar to WPF's FrameworkPropertyMetadata.AffectsRender flag.

GetFlowDirection(Visual)

Gets the value of the attached FlowDirectionProperty on a control.

public static FlowDirection GetFlowDirection(Visual visual)

Parameters

visual Visual

The control.

Returns

FlowDirection

The flow direction.

InvalidateMirrorTransform()

Computes the HasMirrorTransform value according to the FlowDirection and BypassFlowDirectionPolicies

protected virtual void InvalidateMirrorTransform()

InvalidateVisual()

Invalidates the visual and queues a repaint.

public void InvalidateVisual()

LogicalChildrenCollectionChanged(object?, NotifyCollectionChangedEventArgs)

protected override void LogicalChildrenCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)

Parameters

sender object
e NotifyCollectionChangedEventArgs

OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is added to a rooted visual tree.

protected virtual void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs)

Calls the OnAttachedToVisualTree(VisualTreeAttachmentEventArgs) method for this control and all of its visual descendants.

protected virtual void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is removed from a rooted visual tree.

protected virtual void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs)

Calls the OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs) method for this control and all of its visual descendants.

protected virtual void OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnPropertyChanged(AvaloniaPropertyChangedEventArgs)

Called when a avalonia property changes on the object.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)

Parameters

change AvaloniaPropertyChangedEventArgs

The property change details.

OnVisualParentChanged(Visual?, Visual?)

Called when the control's visual parent changes.

protected virtual void OnVisualParentChanged(Visual? oldParent, Visual? newParent)

Parameters

oldParent Visual

The old visual parent.

newParent Visual

The new visual parent.

Render(DrawingContext)

Renders the visual to a DrawingContext.

public virtual void Render(DrawingContext context)

Parameters

context DrawingContext

The drawing context.

SetFlowDirection(Visual, FlowDirection)

Sets the value of the attached FlowDirectionProperty on a control.

public static void SetFlowDirection(Visual visual, FlowDirection value)

Parameters

visual Visual

The control.

value FlowDirection

The property value to set.

Events

AttachedToVisualTree

Raised when the control is attached to a rooted visual tree.

public event EventHandler<VisualTreeAttachmentEventArgs>? AttachedToVisualTree

Event Type

EventHandler<VisualTreeAttachmentEventArgs>

DetachedFromVisualTree

Raised when the control is detached from a rooted visual tree.

public event EventHandler<VisualTreeAttachmentEventArgs>? DetachedFromVisualTree

Event Type

EventHandler<VisualTreeAttachmentEventArgs>