Skip to content

Commit

Permalink
Merge pull request #64 from microsoft/datatrigger-fix
Browse files Browse the repository at this point in the history
DataTrigger: Evaluate initial value on element load
  • Loading branch information
brianlagunas authored Dec 12, 2019
2 parents 1269085 + 9da69d6 commit b88a49d
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Microsoft.Xaml.Behaviors/Core/DataTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ public DataTrigger()
{
}

protected override void OnAttached()
{
base.OnAttached();

//fixes issue #11. We want to evaluate the binding's initial value when the element is first loaded
if (AssociatedObject is FrameworkElement element)
{
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 b88a49d

Please sign in to comment.