/// <summary>
        /// Enqueues any elements within the specified tree that have invalid measure, arrange, or styles, but
        /// were previously removed from the queues as a result of being orphaned by a destroyed window.
        /// </summary>
        /// <param name="root">The root of the tree to restore.</param>
        internal void RestoreToQueues(DependencyObject root)
        {
            Contract.Require(root, nameof(root));

            if (root is UIElement uiElement)
            {
                if (!uiElement.IsMeasureValid)
                {
                    MeasureQueue.Enqueue(uiElement);
                }

                if (!uiElement.IsArrangeValid)
                {
                    ArrangeQueue.Enqueue(uiElement);
                }

                if (!uiElement.IsStyleValid)
                {
                    StyleQueue.Enqueue(uiElement);
                }
            }

            VisualTreeHelper.ForEachChild(root, this, (child, state) =>
            {
                ((PresentationFoundation)state).RestoreToQueues(child);
            });
        }
        /// <summary>
        /// Recursively invalidates the measurement state of the
        /// specified element and all of its descendants.
        /// </summary>
        private static void InvalidateMeasureRecursively(UIElement element)
        {
            element.InvalidateMeasure();

            VisualTreeHelper.ForEachChild(element, null, (child, state) =>
            {
                InvalidateMeasureRecursively((UIElement)child);
            });
        }
 /// <inheritdoc/>
 protected override void PositionChildrenOverride()
 {
     VisualTreeHelper.ForEachChild <UIElement>(this, this, (child, state) =>
     {
         var offset = ((ContentPresenter)state).ContentOffset;
         child.Position(offset);
         child.PositionChildren();
     });
 }
        /// <inheritdoc/>
        protected override void PositionChildrenOverride()
        {
            VisualTreeHelper.ForEachChild <UIElement>(this, this, (child, state) =>
            {
                var scrollCP     = (ScrollContentPresenter)state;
                var scrollViewer = scrollCP.TemplatedParent as ScrollViewer;
                var scrollOffset = scrollViewer == null ? Size2D.Zero : new Size2D(-scrollViewer.ContentHorizontalOffset, -scrollViewer.ContentVerticalOffset);

                var frameworkElement = child as FrameworkElement;
                if (frameworkElement != null)
                {
                    child.Position(frameworkElement.TemplatedParent == state ? Size2D.Zero : scrollOffset);
                }
                else
                {
                    child.Position(Size2D.Zero);
                }
                child.PositionChildren();
            });
        }