title: Set DataContext in WPF XAML to a specific object
body: <p>I started a WPF window from a console application. But I have difficulties with the databinding.</p>
  
  <p><strong>TL;DR: Is it possible to refer to a specific (view)model object in the WPF XAML?</strong></p>
  
  <p>This is my Code:</p>
  
  <p><strong>Console.cs</strong>
  A console application starts the view with a static function in the <code>void Main()</code> function</p>
  
  <pre><code>static void StartGUI(ViewModelClass viewmodel)
  {
      Thread thread = new Thread(() =&gt; { new Application().Run(new View.MainWnd(viewmodel)); });
      thread.SetApartmentState(ApartmentState.STA);
      thread.Start();
  }
  </code></pre>
  
  <p>it gets the viewmodel object which has been initiated in the <code>Main()</code>.</p>
  
  <p><strong>ViewModel.cs</strong>
  The viewmodel is the usual impelemtation of the INotifyPropertyChanged interface with a property to be bound to the view.</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Runtime.CompilerServices;
  
  using ConsoleStartsGUI.Model;
  
  namespace ConsoleStartsGUI.ViewModel
  {
      public class ViewModelClass : INotifyPropertyChanged
      {
          ModelClass model = new ModelClass();
  
          public string VMProperty
          {
              get { return model.TestProperty; }
              set
              {
                  model.TestProperty = value;
                  OnPropertyChanged();
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void OnPropertyChanged([CallerMemberName] String propertyName = "")
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p><strong>View.xaml</strong>
  In the view I get my problems: The view should know the same viewmodel as the console application.</p>
  
  <p>Currently I use </p>
  
  <pre><code>&lt;Window.DataContext&gt;
      &lt;ViewModel:ViewModelClass/&gt;
  &lt;/Window.DataContext&gt;
  </code></pre>
  
  <p>to bind the viewmodelclass to the view (this is the result I clicked in the VS designer) as it usually works when I use a WPF project from begin. But this instantiates a new object, which is not the object of the console application. </p>
  
  <p><strong>View.xaml.cs</strong></p>
  
  <pre><code>using System.Windows;
  using ConsoleStartsGUI.ViewModel;
  
  namespace ConsoleStartsGUI.View
  {
      public partial class MainWnd : Window
      {
          public MainWnd(ViewModelClass viewmodel)
          {
              InitializeComponent();
          }
      }
  }
  </code></pre>
  
  <p>Is there a way, to refer to a specific (view)model object in the XAML?</p>
  
  <p>Best regards,
  Martin</p>