Skip to main content

StyledElement Class

Definition

Namespace:Avalonia
Assembly:Avalonia.Base
Package:Avalonia

Extends an Avalonia.Animation.Animatable with the following features:

public class StyledElement

Constructors

NameDescription
StyledElementInitializes a new instance of the Avalonia.StyledElement class.

StyledElement Constructor

Initializes a new instance of the Avalonia.StyledElement class.

public StyledElement()

Methods

NameDescription
ApplyStylingApplies styling to the control if the control is initialized and styling is not already applied.
BeginInitNo summary available.
EndInitNo summary available.
TryGetResourceNo summary available.
Bind (8 overloads)Binds a Avalonia.AvaloniaProperty to an Avalonia.Data.BindingBase. Inherited from AvaloniaObject.
CheckAccessReturns a value indicating whether the current thread is the UI thread. Inherited from AvaloniaObject.
ClearValue (4 overloads)Clears a Avalonia.AvaloniaProperty's local value. Inherited from AvaloniaObject.
CoerceValueCoerces the specified Avalonia.AvaloniaProperty. Inherited from AvaloniaObject.
EqualsCompares two objects using reference equality. Inherited from AvaloniaObject.
GetBaseValueInherited from AvaloniaObject.
GetHashCodeGets the hash code for the object. Inherited from AvaloniaObject.
GetValue (3 overloads)Gets a Avalonia.AvaloniaProperty value. Inherited from AvaloniaObject.
IsAnimatingChecks whether a Avalonia.AvaloniaProperty is animating. Inherited from AvaloniaObject.
IsSetChecks whether a Avalonia.AvaloniaProperty is set on this object. Inherited from AvaloniaObject.
SetCurrentValue (2 overloads)Sets the value of a dependency property without changing its value source. Inherited from AvaloniaObject.
SetValue (3 overloads)Sets a Avalonia.AvaloniaProperty value. Inherited from AvaloniaObject.
VerifyAccessChecks that the current thread is the UI thread and throws if not. Inherited from AvaloniaObject.

ApplyStyling Method

Applies styling to the control if the control is initialized and styling is not already applied.

public bool ApplyStyling()

Returns

bool

A value indicating whether styling is now applied to the control.

Remarks

The styling system will automatically apply styling when required, so it should not usually be necessary to call this method manually.

BeginInit Method

public void BeginInit()

EndInit Method

public void EndInit()

TryGetResource Method

public bool TryGetResource(object key, Avalonia.Styling.ThemeVariant theme, object& value)

Parameters

key object

theme Avalonia.Styling.ThemeVariant

value object&

Returns

bool

Properties

NameDescription
ActualThemeVariantGets the UI theme that is currently used by the element, which might be different than the RequestedThemeVariantProperty.
ClassesGets or sets the styled element's classes.
DataContextGets or sets the control's data context.
IsInitializedGets a value that indicates whether the element has finished initialization.
NameGets or sets the name of the styled element.
ParentGets the styled element's logical parent.
ResourcesGets or sets the styled element's resource dictionary.
StyleKeyGets the type by which the element is styled.
StylesGets the styles for the styled element.
TemplatedParentGets the styled element whose lookless template this styled element is part of.
ThemeGets or sets the theme to be applied to the element.
TransitionsGets or sets the property transitions for the control. Inherited from Animatable.
DispatcherReturns the Avalonia.AvaloniaObject.Dispatcher that this Avalonia.AvaloniaObject is associated with. Inherited from AvaloniaObject.
ItemInherited from AvaloniaObject.

ActualThemeVariant Property

Gets the UI theme that is currently used by the element, which might be different than the RequestedThemeVariantProperty.

public Avalonia.Styling.ThemeVariant ActualThemeVariant { get; set; }

Returns

If current control is contained in the ThemeVariantScope, TopLevel or Application with non-default RequestedThemeVariant, that value will be returned. Otherwise, current OS theme variant is returned.

Classes Property

Gets or sets the styled element's classes.

public Avalonia.Controls.Classes Classes { get; set; }

Remarks

Classes can be used to apply user-defined styling to styled elements, or to allow styled elements that share a common purpose to be easily selected.

DataContext Property

Gets or sets the control's data context.

public object DataContext { get; set; }

Remarks

The data context is an inherited property that specifies the default object that will be used for data binding.

IsInitialized Property

Gets a value that indicates whether the element has finished initialization.

public bool IsInitialized { get; set; }

Remarks

For more information about when IsInitialized is set, see the Avalonia.StyledElement.Initialized event.

Name Property

Gets or sets the name of the styled element.

public string Name { get; set; }

Remarks

An element's name is used to uniquely identify an element within the element's name scope. Once the element is added to a logical tree, its name cannot be changed.

Parent Property

Gets the styled element's logical parent.

public Avalonia.StyledElement Parent { get; set; }

Resources Property

Gets or sets the styled element's resource dictionary.

public Avalonia.Controls.IResourceDictionary Resources { get; set; }

StyleKey Property

Gets the type by which the element is styled.

public Type StyleKey { get; set; }

Remarks

Usually controls are styled by their own type, but there are instances where you want an element to be styled by its base type, e.g. creating SpecialButton that derives from Button and adds extra functionality but is still styled as a regular Button. To change the style for a control class, override the Avalonia.StyledElement.StyleKeyOverride property

Styles Property

Gets the styles for the styled element.

public Avalonia.Styling.Styles Styles { get; set; }

Remarks

Styles for the entire application are added to the Application.Styles collection, but each styled element may in addition define its own styles which are applied to the styled element itself and its children.

TemplatedParent Property

Gets the styled element whose lookless template this styled element is part of.

public Avalonia.AvaloniaObject TemplatedParent { get; set; }

Theme Property

Gets or sets the theme to be applied to the element.

public Avalonia.Styling.ControlTheme Theme { get; set; }

Fields

DataContextProperty Field

Defines the Avalonia.StyledElement.DataContext property.

public Avalonia.StyledProperty<TValue><object> DataContextProperty

NameProperty Field

Defines the Avalonia.StyledElement.Name property.

public Avalonia.DirectProperty<TOwner,TValue><Avalonia.StyledElement, string> NameProperty

ParentProperty Field

Defines the Avalonia.StyledElement.Parent property.

public Avalonia.DirectProperty<TOwner,TValue><Avalonia.StyledElement, Avalonia.StyledElement> ParentProperty

TemplatedParentProperty Field

Defines the Avalonia.StyledElement.TemplatedParent property.

public Avalonia.DirectProperty<TOwner,TValue><Avalonia.StyledElement, Avalonia.AvaloniaObject> TemplatedParentProperty

ThemeProperty Field

Defines the Avalonia.StyledElement.Theme property.

public Avalonia.StyledProperty<TValue><Avalonia.Styling.ControlTheme> ThemeProperty

Events

NameDescription
ActualThemeVariantChangedRaised when the theme variant is changed on the element or an ancestor of the element.
AttachedToLogicalTreeRaised when the styled element is attached to a rooted logical tree.
DataContextChangedOccurs when the Avalonia.StyledElement.DataContext property changes.
DetachedFromLogicalTreeRaised when the styled element is detached from a rooted logical tree.
InitializedOccurs when the styled element has finished initialization.
ResourcesChangedOccurs when a resource in this styled element or a parent styled element has changed.
PropertyChangedRaised when a Avalonia.AvaloniaProperty value changes on this object. Inherited from AvaloniaObject.

ActualThemeVariantChanged Event

Raised when the theme variant is changed on the element or an ancestor of the element.

public event EventHandler ActualThemeVariantChanged

AttachedToLogicalTree Event

Raised when the styled element is attached to a rooted logical tree.

public event EventHandler<Avalonia.LogicalTree.LogicalTreeAttachmentEventArgs> AttachedToLogicalTree

DataContextChanged Event

Occurs when the Avalonia.StyledElement.DataContext property changes.

public event EventHandler DataContextChanged

Remarks

This event will be raised when the Avalonia.StyledElement.DataContext property has changed and all subscribers to that change have been notified.

DetachedFromLogicalTree Event

Raised when the styled element is detached from a rooted logical tree.

public event EventHandler<Avalonia.LogicalTree.LogicalTreeAttachmentEventArgs> DetachedFromLogicalTree

Initialized Event

Occurs when the styled element has finished initialization.

public event EventHandler Initialized

Remarks

The Initialized event indicates that all property values on the styled element have been set. When loading the styled element from markup, it occurs when System.ComponentModel.ISupportInitialize.EndInit is called and the styled element is attached to a rooted logical tree. When the styled element is created by code and System.ComponentModel.ISupportInitialize is not used, it is called when the styled element is attached to the visual tree.

ResourcesChanged Event

Occurs when a resource in this styled element or a parent styled element has changed.

public event EventHandler<Avalonia.Controls.ResourcesChangedEventArgs> ResourcesChanged