@ -30,10 +30,12 @@ namespace Avalonia
ILogical ,
IThemeVariantHost ,
IStyleHost ,
IStyleable ,
ISetLogicalParent ,
ISetInheritanceParent ,
ISupportInitialize
ISupportInitialize ,
#pragma warning disable CS0618 // Type or member is obsolete
IStyleable
#pragma warning restore CS0618 // Type or member is obsolete
{
/// <summary>
/// Defines the <see cref="DataContext"/> property.
@ -217,6 +219,18 @@ namespace Avalonia
/// </remarks>
public Styles Styles = > _ styles ? ? = new Styles ( this ) ;
/// <summary>
/// Gets the type by which the element is styled.
/// </summary>
/// <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 <see cref="StyleKeyOverride"/>
/// property
/// </remarks>
public Type StyleKey = > StyleKeyOverride ;
/// <summary>
/// Gets or sets the styled element's resource dictionary.
/// </summary>
@ -278,6 +292,18 @@ namespace Avalonia
/// </summary>
protected IPseudoClasses PseudoClasses = > Classes ;
/// <summary>
/// Gets the type by which the element is styled.
/// </summary>
/// <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.
/// </remarks>
protected virtual Type StyleKeyOverride = > GetType ( ) ;
/// <summary>
/// Gets a value indicating whether the element is attached to a rooted logical tree.
/// </summary>
@ -309,24 +335,12 @@ namespace Avalonia
/// <inheritdoc/>
IAvaloniaReadOnlyList < string > IStyleable . Classes = > Classes ;
/// <summary>
/// Gets the type by which the styled element is styled.
/// </summary>
/// <remarks>
/// Usually controls are styled by their own type, but there are instances where you want
/// a styled 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.
/// </remarks>
Type IStyleable . StyleKey = > GetType ( ) ;
/// <inheritdoc/>
bool IStyleHost . IsStylesInitialized = > _ styles ! = null ;
/// <inheritdoc/>
IStyleHost ? IStyleHost . StylingParent = > ( IStyleHost ? ) InheritanceParent ;
/// <inheritdoc/>
public virtual void BeginInit ( )
{
@ -669,7 +683,7 @@ namespace Avalonia
// If the Theme property is not set, try to find a ControlTheme resource with our StyleKey.
if ( _ implicitTheme is null )
{
var key = ( ( IStyleable ) this ) . StyleKey ;
var key = GetStyleKey ( this ) ;
if ( this . TryFindResource ( key , out var value ) & & value is ControlTheme t )
_ implicitTheme = t ;
@ -700,6 +714,22 @@ namespace Avalonia
}
}
/// <summary>
/// Internal getter for <see cref="IStyleable.StyleKey"/> so that we only need to suppress the obsolete
/// warning in one place.
/// </summary>
/// <param name="e">The element</param>
/// <remarks>
/// <see cref="IStyleable"/> is obsolete and will be removed in a future version, but for backwards
/// compatibility we need to support code which overrides <see cref="IStyleable.StyleKey"/>.
/// </remarks>
internal static Type GetStyleKey ( StyledElement e )
{
#pragma warning disable CS0618 // Type or member is obsolete
return ( ( IStyleable ) e ) . StyleKey ;
#pragma warning restore CS0618 // Type or member is obsolete
}
private static void DataContextNotifying ( AvaloniaObject o , bool updateStarted )
{
if ( o is StyledElement element )