Class StyledElement
- Namespace
- Avalonia
- Assembly
- Avalonia.Base.dll
Extends an Animatable with the following features:
- An inherited DataContext.
- Implements IStyleable to allow styling to work on the styled element.
- Implements ILogical to form part of a logical tree.
- A collection of class strings for custom styling.
public class StyledElement : Animatable, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed
- Inheritance
-
StyledElement
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
StyledElement()
Initializes a new instance of the StyledElement class.
public StyledElement()
Fields
DataContextProperty
Defines the DataContext property.
public static readonly StyledProperty<object?> DataContextProperty
Field Value
NameProperty
Defines the Name property.
public static readonly DirectProperty<StyledElement, string?> NameProperty
Field Value
ParentProperty
Defines the Parent property.
public static readonly DirectProperty<StyledElement, StyledElement?> ParentProperty
Field Value
TemplatedParentProperty
Defines the TemplatedParent property.
public static readonly DirectProperty<StyledElement, AvaloniaObject?> TemplatedParentProperty
Field Value
ThemeProperty
Defines the Theme property.
public static readonly StyledProperty<ControlTheme?> ThemeProperty
Field Value
Properties
ActualThemeVariant
Gets the UI theme that is currently used by the element, which might be different than the RequestedThemeVariantProperty.
public ThemeVariant ActualThemeVariant { get; }
Property Value
- ThemeVariant
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
Gets or sets the styled element's classes.
public Classes Classes { get; }
Property Value
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
Gets or sets the control's data context.
public object? DataContext { get; set; }
Property Value
Remarks
The data context is an inherited property that specifies the default object that will be used for data binding.
IsInitialized
Gets a value that indicates whether the element has finished initialization.
public bool IsInitialized { get; }
Property Value
Remarks
For more information about when IsInitialized is set, see the Initialized event.
LogicalChildren
Gets the styled element's logical children.
protected IAvaloniaList<ILogical> LogicalChildren { get; }
Property Value
Name
Gets or sets the name of the styled element.
public string? Name { get; set; }
Property Value
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
Gets the styled element's logical parent.
public StyledElement? Parent { get; }
Property Value
PseudoClasses
Gets the Classes collection in a form that allows adding and removing pseudoclasses.
protected IPseudoClasses PseudoClasses { get; }
Property Value
Resources
Gets or sets the styled element's resource dictionary.
public IResourceDictionary Resources { get; set; }
Property Value
StyleKey
Gets the type by which the element is styled.
public Type StyleKey { get; }
Property Value
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 StyleKeyOverride property
StyleKeyOverride
Gets the type by which the element is styled.
protected virtual Type StyleKeyOverride { get; }
Property Value
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. Override this property to change the style for a control class, returning the type that you wish the elements to be styled as.
Styles
Gets the styles for the styled element.
public Styles Styles { get; }
Property Value
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
Gets the styled element whose lookless template this styled element is part of.
public AvaloniaObject? TemplatedParent { get; }
Property Value
Theme
Gets or sets the theme to be applied to the element.
public ControlTheme? Theme { get; set; }
Property Value
Methods
ApplyStyling()
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()
public virtual void BeginInit()
EndInit()
public virtual void EndInit()
InitializeIfNeeded()
protected void InitializeIfNeeded()
LogicalChildrenCollectionChanged(object?, NotifyCollectionChangedEventArgs)
protected virtual void LogicalChildrenCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
Parameters
sender
objecte
NotifyCollectionChangedEventArgs
OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs)
Called when the styled element is added to a rooted logical tree.
protected virtual void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
Parameters
e
LogicalTreeAttachmentEventArgsThe event args.
OnDataContextBeginUpdate()
Called when the DataContext begins updating.
protected virtual void OnDataContextBeginUpdate()
OnDataContextChanged(EventArgs)
Called when the DataContext property changes.
protected virtual void OnDataContextChanged(EventArgs e)
Parameters
e
EventArgsThe event args.
OnDataContextEndUpdate()
Called when the DataContext finishes updating.
protected virtual void OnDataContextEndUpdate()
OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs)
Called when the styled element is removed from a rooted logical tree.
protected virtual void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
Parameters
e
LogicalTreeAttachmentEventArgsThe event args.
OnInitialized()
Called when the control finishes initialization.
protected virtual void OnInitialized()
OnPropertyChanged(AvaloniaPropertyChangedEventArgs)
Called when a avalonia property changes on the object.
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Parameters
change
AvaloniaPropertyChangedEventArgsThe property change details.
TryGetResource(object, ThemeVariant?, out object?)
Tries to find a resource within the object.
public bool TryGetResource(object key, ThemeVariant? theme, out object? value)
Parameters
key
objectThe resource key.
theme
ThemeVariantTheme used to select theme dictionary.
value
objectWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, null.
Returns
- bool
True if the resource if found, otherwise false.
Events
ActualThemeVariantChanged
Raised when the theme variant is changed on the element or an ancestor of the element.
public event EventHandler? ActualThemeVariantChanged
Event Type
AttachedToLogicalTree
Raised when the styled element is attached to a rooted logical tree.
public event EventHandler<LogicalTreeAttachmentEventArgs>? AttachedToLogicalTree
Event Type
DataContextChanged
Occurs when the DataContext property changes.
public event EventHandler? DataContextChanged
Event Type
Remarks
This event will be raised when the DataContext property has changed and all subscribers to that change have been notified.
DetachedFromLogicalTree
Raised when the styled element is detached from a rooted logical tree.
public event EventHandler<LogicalTreeAttachmentEventArgs>? DetachedFromLogicalTree
Event Type
Initialized
Occurs when the styled element has finished initialization.
public event EventHandler? Initialized
Event Type
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 EndInit() is called and the styled element is attached to a rooted logical tree. When the styled element is created by code and ISupportInitialize is not used, it is called when the styled element is attached to the visual tree.
ResourcesChanged
Occurs when a resource in this styled element or a parent styled element has changed.
public event EventHandler<ResourcesChangedEventArgs>? ResourcesChanged