You shouldn't be using a ContentPresenter
directly in your MainWindow
XAML... that is not what they are for. Instead of that, it is more common to use a ContentControl
(which has its own ContentPresenter
inside):
<Window x:Class="MyPrinterAPI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ContentControl Name="ViewControl" Content="{Binding ViewModel}"
ContentTemplate="{StaticResource TESTTemplate}" />
</StackPanel>
</Window>
...
Also, you'd need to name your UserControl
:
<DataTemplate x:Name="TESTTemplate" DataType="{x:Type vmv:VM}">
<vmv:MyView name="View" />
</DataTemplate>
Using this setup, you should be able to do the following (adapted from the How to: Find DataTemplate-Generated Elements page on MSDN):
// Getting the ContentPresenter from the ViewControl
ContentPresenter myContentPresenter =
VisualTreeHelper.GetChild(ViewControl, 0) as ContentPresenter;
if (myContentPresenter != null)
{
// Finding View from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
MyView myView = (MyView)myDataTemplate.FindName("View", myContentPresenter);
// Do something to the myView control here
}