/// <summary> /// Disconnects a child. /// </summary> private void DisconnectChild(int index) { Debug.Assert(_items[index] != null); Visual child = _items[index]; // // -- Approved By The Core Team -- // // Do not allow foreign threads to change the tree. // (This is a noop if this object is not assigned to a Dispatcher.) // child.VerifyAccess(); Visual oldParent = VisualTreeHelper.GetContainingVisual2D(child._parent); int oldParentIndex = child._parentIndex; // It is invalid to modify the children collection that we // might be iterating during a property invalidation tree walk. if (oldParent.IsVisualChildrenIterationInProgress) { throw new InvalidOperationException(SR.Get(SRID.CannotModifyVisualChildrenDuringTreeWalk)); } _items[index] = null; #if DEBUG child._parentIndex = -1; #endif IncrementVersion(); _owner.InternalRemoveVisualChild(child); }
/// <summary> /// Sets the specified visual at the specified index into the child /// collection. It also corrects the parent. /// Note that the function requires that _item[index] == null and it /// also requires that the passed in child is not connected to another Visual. /// </summary> /// <exception cref="ArgumentException">If the new child has already a parent or if the slot a the specified index is not null.</exception> private void ConnectChild(int index, Visual value) { // // -- Approved By The Core Team -- // // Do not allow foreign threads to change the tree. // (This is a noop if this object is not assigned to a Dispatcher.) // // We also need to ensure that the tree is homogenous with respect // to the dispatchers that the elements belong to. // _owner.VerifyAccess(); value.VerifyAccess(); // It is invalid to modify the children collection that we // might be iterating during a property invalidation tree walk. if (_owner.IsVisualChildrenIterationInProgress) { throw new InvalidOperationException(SR.Get(SRID.CannotModifyVisualChildrenDuringTreeWalk)); } Debug.Assert(value != null); Debug.Assert(_items[index] == null); Debug.Assert(value._parent == null); Debug.Assert(!value.IsRootElement); value._parentIndex = index; _items[index] = value; IncrementVersion(); // Notify the Visual tree about the children changes. _owner.InternalAddVisualChild(value); }