protected override void OnAttached() { if (DataContextProvider != null) { TryFindDataContextWhenNested = true; } else { DataContextProvider = new DataContextProvider(); } AssociatedObject.Loaded += AssociatedObject_Loaded; AssociatedObject.Unloaded += AssociatedObject_Unloaded; if (AssociatedObject.DataContext is INotifyOutAnimation) { (AssociatedObject.DataContext as INotifyOutAnimation).RequestOutAnimation += AnimationBehavior_RequestOutAnimation; _notifyOutAnimation = (AssociatedObject.DataContext as INotifyOutAnimation); } else { if (TryFindDataContextWhenNested) { var dc = DataContextProvider.GetDataContextOfType(AssociatedObject); if (dc != null) { dc.RequestOutAnimation += AnimationBehavior_RequestOutAnimation; _notifyOutAnimation = dc; } } } base.OnAttached(); }
void AssociatedObject_Unloaded(object sender, RoutedEventArgs e) { _notifyOutAnimation = null; AssociatedObject.Unloaded -= AssociatedObject_Unloaded; Detach(); }