Skip to content

Commit

Permalink
added unsubscribe in OnDetaching
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlagunas committed Dec 12, 2019
1 parent f828660 commit 9da69d6
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/Microsoft.Xaml.Behaviors/Core/DataTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,35 @@ protected override void OnAttached()
//fixes issue #11. We want to evaluate the binding's initial value when the element is first loaded
if (AssociatedObject is FrameworkElement element)
{
void OnElementLoaded(object sender, RoutedEventArgs e)
{
try
{
this.EvaluateBindingChange(e);
}
finally
{
element.Loaded -= OnElementLoaded;
}
}

element.Loaded += OnElementLoaded;
}
}

protected override void OnDetaching()
{
base.OnDetaching();
UnsubscribeElementLoadedEvent();
}

private void OnElementLoaded(object sender, RoutedEventArgs e)
{
try
{
EvaluateBindingChange(e);
} finally
{
UnsubscribeElementLoadedEvent();
}
}

private void UnsubscribeElementLoadedEvent()
{
if (AssociatedObject is FrameworkElement element)
{
element.Loaded -= OnElementLoaded;
}
}

/// <summary>
/// Called when the binding property has changed.
/// UA_REVIEW:chabiss
Expand Down

0 comments on commit 9da69d6

Please sign in to comment.