This is done differently if it is defined in XAML and in code. To check, you can create a simple project in WPF with one window and add a custom control that has a ControlTemplate defined in XAML by default. Then add the control to your window and add a button whose Click handler contains a breakpoint.
When a breakpoint hits, we can check the user control and the ControlTemplate. The VisualTree property (which is FrameworkElementFactory) is null.
Reflector, StyleHelper.ApplyTemplateContent. if FrameworkElementFactory ( VisualTreeProperty). if-statement ControlTemplate XAML, FrameworkTEmplate.LoadOptimizedTemplateContent.