/// <summary>
/// Adds a child to the container at a certain index.
/// </summary>
public void AddChild(DisplayObject child, int index)
{
if (index >= 0 && index <= _children.Count)
{
SetRequiresRedraw();
if (child.Parent == this)
{
SetChildIndex(child, index); // avoids dispatching events
}
else
{
child.RemoveFromParent();
_children.Insert(index, child);
child.Parent = this;
child.InvokeAdded(child, this);
if (Stage != null)
{
child.BroadcastAddedToStageEvent(this);
}
}
}
else
{
throw new IndexOutOfRangeException("Invalid child index " + index);
}
}