/// <summary> /// Calls the <see cref="OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)"/> method /// for this control and all of its visual descendents. /// </summary> /// <param name="e">The event args.</param> private void NotifyAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { _visualLogger.Verbose("Attached to visual tree"); _isAttachedToVisualTree = true; if (Name != null && e.NameScope != null) { e.NameScope.Register(Name, this); } OnAttachedToVisualTree(e); if (_visualChildren != null) { foreach (Visual child in _visualChildren.OfType <Visual>()) { var ce = child.GetAttachmentEventArgs(e); child.NotifyAttachedToVisualTree(ce); } } }
/// <summary> /// Calls the <see cref="OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs)"/> method /// for this control and all of its visual descendents. /// </summary> /// <param name="e">The event args.</param> private void NotifyDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { _visualLogger.Verbose("Detached from visual tree"); VisualRoot = null; OnDetachedFromVisualTree(e); if (VisualChildren != null) { foreach (Visual child in VisualChildren.OfType<Visual>()) { child.NotifyDetachedFromVisualTree(e); } } }
/// <summary> /// Calls the <see cref="OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)"/> method /// for this control and all of its visual descendents. /// </summary> /// <param name="e">The event args.</param> private void NotifyAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { _visualLogger.Verbose("Attached to visual tree"); VisualRoot = e.Root; OnAttachedToVisualTree(e); if (VisualChildren != null) { foreach (Visual child in VisualChildren.OfType<Visual>()) { child.NotifyAttachedToVisualTree(e); } } }
/// <summary> /// Sets the visual parent of the Visual. /// </summary> /// <param name="value">The visual parent.</param> private void SetVisualParent(Visual value) { if (_visualParent == value) { return; } var old = _visualParent; _visualParent = value; if (VisualRoot != null) { var e = new VisualTreeAttachmentEventArgs(VisualRoot); NotifyDetachedFromVisualTree(e); } if (_visualParent is IRenderRoot || _visualParent?.IsAttachedToVisualTree == true) { var root = this.GetVisualAncestors().OfType<IRenderRoot>().FirstOrDefault(); var e = new VisualTreeAttachmentEventArgs(root); NotifyAttachedToVisualTree(e); } RaisePropertyChanged(VisualParentProperty, old, value, BindingPriority.LocalValue); }
/// <summary> /// Called when the control is removed from a visual tree. /// </summary> /// <param name="e">The event args.</param> /// <remarks> /// It is vital that if you override this method you call the base implementation; /// failing to do so will cause numerous features to not work as expected. /// </remarks> protected virtual void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { if (RenderTransform != null) { RenderTransform.Changed -= RenderTransformChanged; } DetachedFromVisualTree?.Invoke(this, e); }
/// <summary> /// Calls the <see cref="OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs)"/> method /// for this control and all of its visual descendents. /// </summary> /// <param name="e">The event args.</param> private void NotifyDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { _visualLogger.Verbose("Detached from visual tree"); if (Name != null && e.NameScope != null) { e.NameScope.Unregister(Name); } _isAttachedToVisualTree = false; OnDetachedFromVisualTree(e); if (_visualChildren != null) { foreach (Visual child in _visualChildren.OfType<Visual>()) { var ce = child.GetAttachmentEventArgs(e); child.NotifyDetachedFromVisualTree(ce); } } }
/// <summary> /// Calls the <see cref="OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)"/> method /// for this control and all of its visual descendents. /// </summary> /// <param name="e">The event args.</param> private void NotifyAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { _visualLogger.Verbose("Attached to visual tree"); _isAttachedToVisualTree = true; if (Name != null && e.NameScope != null) { e.NameScope.Register(Name, this); } OnAttachedToVisualTree(e); if (_visualChildren != null) { foreach (Visual child in _visualChildren.OfType<Visual>()) { var ce = child.GetAttachmentEventArgs(e); child.NotifyAttachedToVisualTree(ce); } } }
/// <summary> /// Gets the <see cref="VisualTreeAttachmentEventArgs"/> for this element based on the /// parent's args. /// </summary> /// <param name="e">The parent args.</param> /// <returns>The args for this element.</returns> private VisualTreeAttachmentEventArgs GetAttachmentEventArgs(VisualTreeAttachmentEventArgs e) { var childNameScope = (this as INameScope) ?? NameScope.GetNameScope(this); if (childNameScope != null) { return new VisualTreeAttachmentEventArgs(e.Root, childNameScope); } else { return e; } }
/// <summary> /// Called when the control is removed from a visual tree. /// </summary> /// <param name="e">The event args.</param> protected virtual void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { DetachedFromVisualTree?.Invoke(this, e); }
/// <summary> /// Called when the control is added to a visual tree. /// </summary> /// <param name="e">The event args.</param> protected virtual void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { AttachedToVisualTree?.Invoke(this, e); }