public void ClearElementToElementFactory(UIElement element) { m_owner.OnElementClearing(element); if (m_owner.ItemTemplateShim != null) { if (m_ElementFactoryRecycleArgs == null) { // Create one. m_ElementFactoryRecycleArgs = new ElementFactoryRecycleArgs(); } var context = m_ElementFactoryRecycleArgs; context.Element = element; context.Parent = m_owner; m_owner.ItemTemplateShim.RecycleElement(context); context.Element = null; context.Parent = null; } else { // No ItemTemplate to recycle to, remove the element from the children collection. var children = m_owner.Children; int childIndex = 0; childIndex = children.IndexOf(element); if (childIndex < 0) { throw new InvalidOperationException("ItemsRepeater's child not found in its Children collection."); } children.RemoveAt(childIndex); } var virtInfo = ItemsRepeater.GetVirtualizationInfo(element); virtInfo.MoveOwnershipToElementFactory(); m_phaser.StopPhasing(element, virtInfo); if (m_lastFocusedElement == element) { // Focused element is going away. Remove the tracked last focused element // and pick a reasonable next focus if we can find one within the layout // realized elements. int clearedIndex = virtInfo.Index; MoveFocusFromClearedIndex(clearedIndex); } REPEATER_TRACE_PERF("ElementCleared"); }