Skip to content

Commit

Permalink
Set logical parent or visual tree attachment/detachment if Associated…
Browse files Browse the repository at this point in the history
…Object was set
  • Loading branch information
wieslawsoltes committed Oct 30, 2024
1 parent 165614d commit 39e8326
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Avalonia.Xaml.Interactivity/StyledElementBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,31 @@ protected virtual void OnDetaching()
{
}

void IBehaviorEventsHandler.AttachedToVisualTreeEventHandler() => OnAttachedToVisualTree();
void IBehaviorEventsHandler.AttachedToVisualTreeEventHandler()
{
AttachBehaviorToLogicalTree();

OnAttachedToVisualTree();
}

void IBehaviorEventsHandler.DetachedFromVisualTreeEventHandler() => OnDetachedFromVisualTree();
void IBehaviorEventsHandler.DetachedFromVisualTreeEventHandler()
{
DetachBehaviorFromLogicalTree();

OnDetachedFromVisualTree();
}

void IBehaviorEventsHandler.AttachedToLogicalTreeEventHandler()
{
AttachToLogicalTree();
AttachBehaviorToLogicalTree();

OnAttachedToLogicalTree();
}

void IBehaviorEventsHandler.DetachedFromLogicalTreeEventHandler()
{
DetachFromLogicalTree();
DetachBehaviorFromLogicalTree();

OnDetachedFromLogicalTree();
}

Expand Down Expand Up @@ -175,18 +187,21 @@ protected virtual void OnUnloaded()
{
}

private void AttachToLogicalTree()
private void AttachBehaviorToLogicalTree()
{
if (AssociatedObject is not StyledElement styledElement)
{
return;
}

((ISetLogicalParent)this).SetParent(null);
((ISetLogicalParent)this).SetParent(styledElement);
if (styledElement.Parent is not null)
{
((ISetLogicalParent)this).SetParent(null);
((ISetLogicalParent)this).SetParent(styledElement);
}
}

private void DetachFromLogicalTree()
private void DetachBehaviorFromLogicalTree()
{
((ISetLogicalParent)this).SetParent(null);
}
Expand Down

0 comments on commit 39e8326

Please sign in to comment.