Set UMG ViewModel for UUserWidget C++

Apologies for necro-ing this post again, but since it’s one of the first results on google, putting this here for future-me:

The system uses userwidget extensions.

if (UMVVMView* view = Cast<UMVVMView>(userWidget->GetExtension(UMVVMView::StaticClass())))
{
	FName vmName("MyViewModel");
	if (view->GetViewModel(vmName) == nullptr)
	{
		view->SetViewModel(vmName, LifeTime);
	}
}