@ -59,7 +59,7 @@ namespace Avalonia
private int _ initCount ;
private int _ initCount ;
private string _ name ;
private string _ name ;
private readonly Classes _ classes = new Classes ( ) ;
private readonly Classes _ classes = new Classes ( ) ;
private bool _ isAttachedToLogicalTree ;
private IStyleRoot _ styleRoot ;
private IAvaloniaList < ILogical > _l ogicalChildren ;
private IAvaloniaList < ILogical > _l ogicalChildren ;
private IResourceDictionary _ resources ;
private IResourceDictionary _ resources ;
private Styles _ styles ;
private Styles _ styles ;
@ -81,7 +81,7 @@ namespace Avalonia
/// </summary>
/// </summary>
public StyledElement ( )
public StyledElement ( )
{
{
_ isAttachedToLogicalTree = this i s IStyleRoot ;
_ styleRoot = this a s IStyleRoot ;
}
}
/// <summary>
/// <summary>
@ -307,7 +307,7 @@ namespace Avalonia
/// <summary>
/// <summary>
/// Gets a value indicating whether the element is attached to a rooted logical tree.
/// Gets a value indicating whether the element is attached to a rooted logical tree.
/// </summary>
/// </summary>
bool ILogical . IsAttachedToLogicalTree = > _ isAttachedToLogicalTree ;
bool ILogical . IsAttachedToLogicalTree = > _ styleRoot ! = null ;
/// <summary>
/// <summary>
/// Gets the styled element's logical parent.
/// Gets the styled element's logical parent.
@ -367,7 +367,7 @@ namespace Avalonia
throw new InvalidOperationException ( "BeginInit was not called." ) ;
throw new InvalidOperationException ( "BeginInit was not called." ) ;
}
}
if ( - - _ initCount = = 0 & & _ isAttachedToLogicalTree )
if ( - - _ initCount = = 0 & & _ styleRoot ! = null )
{
{
InitializeStylesIfNeeded ( ) ;
InitializeStylesIfNeeded ( ) ;
@ -435,19 +435,6 @@ namespace Avalonia
throw new InvalidOperationException ( "The Control already has a parent." ) ;
throw new InvalidOperationException ( "The Control already has a parent." ) ;
}
}
if ( _ isAttachedToLogicalTree )
{
var oldRoot = FindStyleRoot ( old ) ? ? this as IStyleRoot ;
if ( oldRoot = = null )
{
throw new AvaloniaInternalException ( "Was attached to logical tree but cannot find root." ) ;
}
var e = new LogicalTreeAttachmentEventArgs ( oldRoot ) ;
OnDetachedFromLogicalTreeCore ( e ) ;
}
if ( InheritanceParent = = null | | parent = = null )
if ( InheritanceParent = = null | | parent = = null )
{
{
InheritanceParent = parent as AvaloniaObject ;
InheritanceParent = parent as AvaloniaObject ;
@ -455,6 +442,12 @@ namespace Avalonia
Parent = ( IStyledElement ) parent ;
Parent = ( IStyledElement ) parent ;
if ( _ styleRoot ! = null )
{
var e = new LogicalTreeAttachmentEventArgs ( _ styleRoot , this , old ) ;
OnDetachedFromLogicalTreeCore ( e ) ;
}
if ( old ! = null )
if ( old ! = null )
{
{
old . ResourcesChanged - = ThisResourcesChanged ;
old . ResourcesChanged - = ThisResourcesChanged ;
@ -474,7 +467,7 @@ namespace Avalonia
throw new AvaloniaInternalException ( "Parent is attached to logical tree but cannot find root." ) ;
throw new AvaloniaInternalException ( "Parent is attached to logical tree but cannot find root." ) ;
}
}
var e = new LogicalTreeAttachmentEventArgs ( newRoot ) ;
var e = new LogicalTreeAttachmentEventArgs ( newRoot , this , parent ) ;
OnAttachedToLogicalTreeCore ( e ) ;
OnAttachedToLogicalTreeCore ( e ) ;
}
}
@ -716,9 +709,9 @@ namespace Avalonia
// - ListBox makes ListBoxItem a logical child
// - ListBox makes ListBoxItem a logical child
// - ListBox template gets applied; making its Panel get attached to logical tree
// - ListBox template gets applied; making its Panel get attached to logical tree
// - That AttachedToLogicalTree signal travels down to the ListBoxItem
// - That AttachedToLogicalTree signal travels down to the ListBoxItem
if ( ! _ isAttachedToLogicalTree )
if ( _ styleRoot = = null )
{
{
_ isAttachedToLogicalTree = true ;
_ styleRoot = e . Root ;
InitializeStylesIfNeeded ( true ) ;
InitializeStylesIfNeeded ( true ) ;
@ -734,9 +727,9 @@ namespace Avalonia
private void OnDetachedFromLogicalTreeCore ( LogicalTreeAttachmentEventArgs e )
private void OnDetachedFromLogicalTreeCore ( LogicalTreeAttachmentEventArgs e )
{
{
if ( _ isAttachedToLogicalTree )
if ( _ styleRoot ! = null )
{
{
_ isAttachedToLogicalTree = false ;
_ styleRoot = null ;
_ styleDetach . OnNext ( this ) ;
_ styleDetach . OnNext ( this ) ;
OnDetachedFromLogicalTree ( e ) ;
OnDetachedFromLogicalTree ( e ) ;
DetachedFromLogicalTree ? . Invoke ( this , e ) ;
DetachedFromLogicalTree ? . Invoke ( this , e ) ;