title: ObjectDataProvider in XAML with ObjectInstance doesn't call ViewModel Contructor in debug
body: <p>We used to do this at my previous job and it worked perfect there, but for some reason I cannot get it to work now.</p>
  
  <p>I just want to use an ObjectDataProvider to create an instance of the ViewModel class in XAML so I can reference it for binding, and I've got the DataContext set on the Window to the ViewModel, and I've got a xmlns:local with a fully qualified name with the same namespace as all of my ViewModels.</p>
  
  <pre><code>&lt;Window 
  x:Class="TimersXP.TimersHost"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  xmlns:local="clr-namespace:TimersXP.ViewModels"
  Name="TimersHostView"
  SizeToContent="Height"
  Title="TimersXP"
  WindowStartupLocation="CenterScreen"
  WindowStyle="ToolWindow"
  DataContext="TimersHostViewModel"&gt;
  &lt;Window.Resources&gt;
      &lt;ObjectDataProvider x:Key="TimersHostViewModel" ObjectInstance="{x:Type local:TimersHostViewModel}"/&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Then later my attempt to use the ViewModel:</p>
  
  <pre><code>&lt;Grid.ContextMenu&gt;
          &lt;ContextMenu ItemsSource="{Binding Source={StaticResource TimersHostViewModel}, Path=Skins}" Style="{DynamicResource styleBanner}"/&gt;
  &lt;/Grid.ContextMenu&gt;
  </code></pre>
  
  <p>But my problem is that when I set break points in the TimersHostViewModel parameter-less constructor, they are never hit when debugging. So it seems that the ObjectDataProvider isn't doing it's job correctly. Although the window does get created.</p>
  
  <p>I can see that the break points in my App.xaml.cs and MainWindow.xaml.cs are hit, InitializeComponent is also hit, and even my singleton model class is created. But I cannot for the life of me figure out why my TimersHostViewModel class constructor is never called.</p>
  
  <p>One thing I remember is that in my job we used a line like this:
  </p>
  
  <p>I see that there is no ObjectInstance, but only ObjectType, so when I tried to remove ObjectInstance and set the ObjectType instead, I get the following error:
  Object reference not set to an instance of an object, and it is highlighting the ObjectType="{x:Type local:TimersHostViewModel}"...What gives?! Why does it work in one instance and not another? I'm very confused about that.</p>
  
  <p>EDIT: Ok I was able to partly answer my own question, but the real question still remains.
  So I was able to get the ViewModel constructor to be called by removing the DataContext for the Window, and changing the ObjectDataProvider ObjectInstance to ObjectType. Then setting the Grid DataContext to bind to the TimersHostViewModel. But that still leaves me now with this design-time/Compile-time error on the ObjectDataProvider ObjectType: Object reference not set to an instance of an object.</p>
  
  <pre><code>&lt;Window 
  x:Class="TimersXP.TimersHost"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
  xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  xmlns:local="clr-namespace:TimersXP.ViewModels"
  Name="TimersHostView"
  SizeToContent="Height"
  Title="TimersXP"
  WindowStartupLocation="CenterScreen"
  WindowStyle="ToolWindow"&gt;
  &lt;Window.Resources&gt;
      &lt;ObjectDataProvider x:Key="TimersHostViewModel" ObjectType="{x:Type local:TimersHostViewModel}"/&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid DataContext="{Binding Source={StaticResource TimersHostViewModel}}"&gt;
  </code></pre>
  
  <p>Also still not sure why it would work under some circumstances but not others, like the one I'm using now.</p>
  
  <p>Full source code here:
  <a href="http://timersxp.codeplex.com/SourceControl/latest#VS2013/TimersXP/Views/TimersHost.xaml" rel="nofollow">http://timersxp.codeplex.com/SourceControl/latest#VS2013/TimersXP/Views/TimersHost.xaml</a></p>