title: Binding UserControl View model on MainWindow with MVVM
body: <p>I am newbie to WPF and MVVM and I am trying to learn how WPF working with MVVM.  For that,  I have made a sample as follows</p>
  
  <h2><strong>UserControl1.xaml</strong></h2>
  
  <pre><code>&lt;StackPanel&gt;
          &lt;TextBox   Text="{Binding MyString}" /&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <h2><strong>UserControl1ViewModel.cs</strong></h2>
  
  <pre><code>class UserControl1ViewModel
  {
       public string MyString { get; set; }
  }
  </code></pre>
  
  <h2><strong>MainWindow.xaml</strong></h2>
  
  <pre><code>&lt;StackPanel&gt;
          &lt;local:UserControl1 DataContext="{Binding UC1Property}"/&gt;  //tried binding the Usercontrol1VM obj on MainWindowVM
          &lt;Button Command="{Binding ShowMeOne}" Height="30" Content="ShowOne"/&gt;
          &lt;Button Command="{Binding ShowMeAnother}" Height="30" Content="ShowAnother" /&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <h2><strong>MainWindow.xaml.cs</strong></h2>
  
  <pre><code>public MainWindow()
  {
     InitializeComponent();
     this.DataContext = new MainWindowViewModel();
  }
  </code></pre>
  
  <h2><strong>MainWindowViewModel.cs</strong></h2>
  
  <pre><code>class MainWindowViewModel
  {
      public MainWindowViewModel()
      {
          ShowMeOne = new RelayCommand(Prompt_ShowMeOne);
          ShowMeAnother = new RelayCommand(Prompt_ShowMeAnother);
          UC1Property.MyString = "Initial";
      }
  
      private void Prompt_ShowMeAnother(object obj)
      {
          global::System.Windows.MessageBox.Show("Another Should be shown");     
          UC1Property.MyString = "Last Clicked:  Another";
      }
  
      private void Prompt_ShowMeOne(object obj)
      {
          global::System.Windows.MessageBox.Show("One Should be shown");
          UC1Property.MyString = "Last Clicked:  One";
      }
  
      public ICommand ShowMeOne { get; set; }
      public ICommand ShowMeAnother { get; set; }
  
  
      //UserControl1 View Model for MainWindow
      public UserControl1ViewModel UC1Property { get; set; }
  
  
  }
  </code></pre>
  
  <p><strong><em>Problem:</em></strong> Now, how can I pass the Datacontext of the Usercontrol into the MainWindow?</p>
  
  <pre><code>-----------------------------In MainWindow.xaml----------------------
  &lt;local:UserControl1 DataContext="{Binding UC1Property}"/&gt;  //tried binding the Usercontrol1VM obj on MainWindowVM
  -----------------------------In MainWindowViewModel.cs---------------
  //UserControl1 View Model for MainWindow
  public UserControl1ViewModel UC1Property { get; set; }
  </code></pre>
  
  <p>Above code which I tried is not working as expected.  What is the standard way of passing the datacontext of usercontrol over the window?</p>