/// <summary>
/// Invoked when the VisualCollection of a visual object is modified.
/// </summary>
/// <param name="visualAdded">The Visual that was added to the collection.</param>
/// <param name="visualRemoved">The Visual that was removed from the collection.</param>
protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
{
if (!_ignoreVisualChange)
{
// Let base class do its own stuff
base.OnVisualChildrenChanged(visualAdded, visualRemoved);
// When an items host we do not get the UIElementAdded/Removed events
// for changing the element state dictionary. So we do it here instead.
if (IsItemsHost)
{
if (visualAdded is UIElement)
{
UIElement elementAdded = (UIElement)visualAdded;
_stateDict.Add(elementAdded, new MetaElementState(elementAdded));
}
if (visualRemoved is UIElement)
{
UIElement elementRemoved = (UIElement)visualRemoved;
MetaElementState elementState = _stateDict[elementRemoved];
// If item has finished its removal
if (elementState.Status == MetaElementStatus.Removing)
{
// Base class already removed it as a visual/logical child so just remove dictionary entry
_stateDict.Remove(elementRemoved);
}
else
{
// Item needs marking so it removal animates
elementState.Status = MetaElementStatus.Removing;
elementState.TargetChanged = true;
// Prevent reentrancy from trying to process the element being added back again
_ignoreVisualChange = true;
// Add into the internal collection and add back as a visual child
_children.InternalAdd(elementRemoved);
_ignoreVisualChange = false;
}
}
}
}
}