void BehaviorCollection::OnVectorChanged()

in src/BehaviorsSDKNative/Microsoft.Xaml.Interactivity/BehaviorCollection.cpp [25:95]


void BehaviorCollection::OnVectorChanged(IObservableVector<DependencyObject^>^ sender, IVectorChangedEventArgs^ eventArgs)
{
	if (eventArgs->CollectionChange == CollectionChange::Reset)
	{
		for (auto& behavior : this->oldCollection)
		{
			if (behavior->AssociatedObject != nullptr)
			{
				behavior->Detach();
			}
		}

		this->oldCollection.clear();
		this->oldCollection.reserve(this->Size);

		for (const auto& newItem : this)
		{
			this->oldCollection.push_back(this->VerifiedAttach(newItem));
		}

#if _DEBUG
		this->VerifyOldCollectionIntegrity();
#endif

		return;
	}

	unsigned int eventIndex = eventArgs->Index;
	DependencyObject^ changedItem = this->GetAt(eventIndex);

	switch (eventArgs->CollectionChange)
	{
	case CollectionChange::ItemInserted:
		{
			this->oldCollection.insert(oldCollection.begin() + eventIndex, this->VerifiedAttach(changedItem));
		}
		break;

	case CollectionChange::ItemChanged:
		{
			IBehavior^ oldItem = this->oldCollection[eventIndex];
			if (oldItem->AssociatedObject != nullptr)
			{
				oldItem->Detach();
			}

			this->oldCollection[eventIndex] = this->VerifiedAttach(changedItem);
		}
		break;

	case CollectionChange::ItemRemoved:
		{
			IBehavior^ oldItem = this->oldCollection[eventIndex];
			if (oldItem->AssociatedObject != nullptr)
			{
				oldItem->Detach();
			}

			this->oldCollection.erase(oldCollection.begin() + eventIndex);
		}
		break;

	default:
		_ASSERT(false);
		break;
	}

#if _DEBUG
	this->VerifyOldCollectionIntegrity();
#endif
}