public override void Insert(int index, UIElement element)
{
if (_isItemsHost)
base.Insert(index, element);
else
{
ValidateElement(element);
VerifyWriteAccess();
// Add to the external list
_externalChildren.Insert(index, element);
// Add to internal logical tree
SetLogicalParent(element);
// Add to the internal visual tree (in same relative position in the visual collection)
if (_internalChildren.Count == 0)
_internalChildren.Add(element);
else if (index == 0)
_internalChildren.Insert(0, element);
else
_internalChildren.Insert(_internalChildren.IndexOf(_externalChildren[index - 1]) + 1, element);
// Instruct the visual parent it must measure with the new child element
_visualParent.InvalidateMeasure();
// Added event is always generated after the base call
OnUIElementsAdded(new UIElementsEventArgs(element));
}
}