title: Designing WPF UserControl that gets its DataContext from outer controls: How to have some sample data in designer but use inherited DC at runtime?
body: <p>I am designing a WPF user control which contains other user controls (imagine a WidgetContainer, containing different Widgets) - using M-V-VM architecture. 
  During development, I have WidgetContainerView in a window, window (View) spawns a WidgetContainerViewModel as its resource, and in a parameterless constructor of WidgetContainerViewModel, I fill its exposed collection with some sample widgets (WidgetViewModels).</p>
  
  <p>WidgetContainer control inherits the DataContext from window, and inside, there is a ListView, that binds Widgets to WidgetView control (which is inside ListView.ItemTemplate).</p>
  
  <p>Now this works OK in my WindowView, as I see my sample widgets, but once I edit the WidgetContainerView  or WidgetView, there is no content - at design time, controls are standalone, and they don't inherit any DataContext, so I don't see a content, and have troubles designing them (a ListView is empty, Widget's fields as well...).</p>
  
  <p>I tried adding a sample widget to the WidgetView:</p>
  
  <pre><code>public partial class WidgetView : UserControl
  {
      public WidgetView()
      {
          InitializeComponent();
          if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
          {
              //btw, MessageBox.Show(...) here sometimes crashes my Visual Studio (2008), but I have seen the message - this code gets executed at design time, but with some lag - I saw the message on reload of designer, but at that time, I have already commented it - wtf?
              this.DataContext = new WidgetViewModel(); //creates sample widget
          }
      }
  }
  </code></pre>
  
  <p>but that didn't work - I still don't see anything in designer.</p>
  
  <p>I also wanted to create a WidgetViewModel as a resource in WidgetView, like this:</p>
  
  <pre><code>&lt;UserControl x:Class="MVVMTestWidgetsControl.View.WidgetView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      DataContext="WidgetViewModel" //this doesn't work!
      Height="Auto" Width="Auto"&gt;
      &lt;UserControl.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ViewModel:WidgetViewModel x:Key="WidgetViewModel" /&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;TextBlock Text="{Binding Path=Title}"&gt;&lt;/TextBlock&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>but I don't know how to assign a WidgetViewModel as a DataContext of a whole widget - I can't add DataContext attribute to UserControl, because WidgetViewModel is defined later in the code. Any ideas how to do this? I could use a sample data this way, and just override it in code so that it has the right content at runtime...</p>
  
  <p>What are your best practices when developing user controls? Thank you, designing empty control is no fun :)).</p>