title: WPF Usercontrol databinding with MVVM ViewModel
body: <p>I have spent quite some time on this so seeking help.</p>
  
  <p>Simple data binding with usercontrol with mvvm light does not work.</p>
  
  <p>I have done the following.</p>
  
  <ol>
  <li>Created a MvvmLight (WPF451) project using VS 2015 and named it WpfDataBindingUserControlT1</li>
  <li>Added a UserControl and renamed it to SimpleUserControl.xaml</li>
  <li>Added some lables as a children(wraped in stackpanel) to the grid inside SimpleUserControl.xaml(All code is given below)</li>
  <li>Added a dependency properties in the code behind of the SimpleUserControl.xaml(SimpleUserControl.cs) so that these will help me in databinding.</li>
  </ol>
  
  <p>The data binding simply does not work. I have pulled half of my hair on this so please help. I guess I am missing very simple on this.</p>
  
  <p>The code is as follows.</p>
  
  <ol>
  <li><p>MainWindows.xaml</p>
  
  <pre><code>&lt;Window x:Class="WpfDataBindingUserControlT1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:ignore="http://www.galasoft.ch/ignore"
          xmlns:local="clr-namespace:WpfDataBindingUserControlT1"
          mc:Ignorable="d ignore"
          Height="400"
          Width="300"
          Title="MVVM Light Application"
          DataContext="{Binding Main, Source={StaticResource Locator}}"&gt;
  
      &lt;Window.Resources&gt;
          &lt;ResourceDictionary&gt;
              &lt;ResourceDictionary.MergedDictionaries&gt;
                  &lt;ResourceDictionary Source="Skins/MainSkin.xaml" /&gt;
              &lt;/ResourceDictionary.MergedDictionaries&gt;
          &lt;/ResourceDictionary&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid x:Name="LayoutRoot"&gt;
  
          &lt;TextBlock FontSize="36"
                     FontWeight="Bold"
                     Foreground="Purple"
                     Text="{Binding WelcomeTitle}"
                     VerticalAlignment="Center"
                     HorizontalAlignment="Center"
                     TextWrapping="Wrap" /&gt;
  
          &lt;local:SimpleUserControl DataContext="{Binding RelativeSource={RelativeSource Self}}" CellValue="{Binding WelcomeTitle}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre></li>
  <li><p>MainWindow.cs ( I did not change any thing in this file.)</p>
  
  <pre><code>using System.Windows;
  using WpfDataBindingUserControlT1.ViewModel;
  
  namespace WpfDataBindingUserControlT1
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          /// &lt;summary&gt;
          /// Initializes a new instance of the MainWindow class.
          /// &lt;/summary&gt;
          public MainWindow()
          {
              InitializeComponent();
              Closing += (s, e) =&gt; ViewModelLocator.Cleanup();
          }
      }
  }
  </code></pre></li>
  <li><p>SimpleUserControl.xaml</p>
  
  <pre><code>&lt;UserControl x:Class="WpfDataBindingUserControlT1.SimpleUserControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:WpfDataBindingUserControlT1"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;Grid&gt;
          &lt;StackPanel&gt;
              &lt;Label Content="This Prints" /&gt;
              &lt;Label Name="MyLable" Content="{Binding Path=CellValue}"&gt;&lt;/Label&gt;
              &lt;Label Content="This also Prints" /&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre></li>
  <li><p>SimpleUserControl.cs (added a dependency prop)</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  
  namespace WpfDataBindingUserControlT1
  {
      /// &lt;summary&gt;
      /// Interaction logic for SimpleUserControl.xaml
      /// &lt;/summary&gt;
      public partial class SimpleUserControl : UserControl
      {
          public string CellValue
          {
              get { return (string)GetValue(CellValueProperty); }
              set { SetValue(CellValueProperty, value); }
          }
  
          public static readonly DependencyProperty CellValueProperty = 
              DependencyProperty.Register("CellValue", typeof(string), typeof(SimpleUserControl), new FrameworkPropertyMetadata
              {
                  BindsTwoWayByDefault = true,
              });
  
          public SimpleUserControl()
          {
              InitializeComponent();
          }
      }
  }
  </code></pre></li>
  <li><p>MainViewModel.cs (I have not changed any thing in this)</p>
  
  <pre><code>using GalaSoft.MvvmLight;
  using WpfDataBindingUserControlT1.Model;
  
  namespace WpfDataBindingUserControlT1.ViewModel
  {
      /// &lt;summary&gt;
      /// This class contains properties that the main View can data bind to.
      /// &lt;para&gt;
      /// See http://www.mvvmlight.net
      /// &lt;/para&gt;
      /// &lt;/summary&gt;
      public class MainViewModel : ViewModelBase
      {
          private readonly IDataService _dataService;
  
          /// &lt;summary&gt;
          /// The &lt;see cref="WelcomeTitle" /&gt; property's name.
          /// &lt;/summary&gt;
          public const string WelcomeTitlePropertyName = "WelcomeTitle";
  
          private string _welcomeTitle = string.Empty;
  
          /// &lt;summary&gt;
          /// Gets the WelcomeTitle property.
          /// Changes to that property's value raise the PropertyChanged event. 
          /// &lt;/summary&gt;
          public string WelcomeTitle
          {
              get
              {
                  return _welcomeTitle;
              }
              set
              {
                  Set(ref _welcomeTitle, value);
              }
          }
  
          /// &lt;summary&gt;
          /// Initializes a new instance of the MainViewModel class.
          /// &lt;/summary&gt;
          public MainViewModel(IDataService dataService)
          {
              _dataService = dataService;
              _dataService.GetData(
                  (item, error) =&gt;
                  {
                      if (error != null)
                      {
                          // Report error here
                          return;
                      }
  
                      WelcomeTitle = item.Title;
                  });
          }
  
          ////public override void Cleanup()
          ////{
          ////    // Clean up if needed
  
          ////    base.Cleanup();
          ////}
      }
  }
  </code></pre></li>
  <li><p>ViewModelLocator.cs (I have not changed any thing in this as well.)</p>
  
  <pre><code>/*
    In App.xaml:
    &lt;Application.Resources&gt;
        &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:WpfDataBindingUserControlT1.ViewModel"
                                     x:Key="Locator" /&gt;
    &lt;/Application.Resources&gt;
  
    In the View:
    DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  */
  
  using GalaSoft.MvvmLight;
  using GalaSoft.MvvmLight.Ioc;
  using Microsoft.Practices.ServiceLocation;
  using WpfDataBindingUserControlT1.Model;
  
  namespace WpfDataBindingUserControlT1.ViewModel
  {
      /// &lt;summary&gt;
      /// This class contains static references to all the view models in the
      /// application and provides an entry point for the bindings.
      /// &lt;para&gt;
      /// See http://www.mvvmlight.net
      /// &lt;/para&gt;
      /// &lt;/summary&gt;
      public class ViewModelLocator
      {
          static ViewModelLocator()
          {
              ServiceLocator.SetLocatorProvider(() =&gt; SimpleIoc.Default);
  
              if (ViewModelBase.IsInDesignModeStatic)
              {
                  SimpleIoc.Default.Register&lt;IDataService, Design.DesignDataService&gt;();
              }
              else
              {
                  SimpleIoc.Default.Register&lt;IDataService, DataService&gt;();
              }
  
              SimpleIoc.Default.Register&lt;MainViewModel&gt;();
          }
  
          /// &lt;summary&gt;
          /// Gets the Main property.
          /// &lt;/summary&gt;
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
              "CA1822:MarkMembersAsStatic",
              Justification = "This non-static member is needed for data binding purposes.")]
          public MainViewModel Main
          {
              get
              {
                  return ServiceLocator.Current.GetInstance&lt;MainViewModel&gt;();
              }
          }
  
          /// &lt;summary&gt;
          /// Cleans up all the resources.
          /// &lt;/summary&gt;
          public static void Cleanup()
          {
          }
      }
  }
  </code></pre></li>
  </ol>