title: WPF Bind Window Title to ViewModel Property
body: <p>I am trying to bind a Window Title to the ViewModel which has a Title property.  Below is the MainWindow XAML:</p>
  
  <pre><code>&lt;Window x:Class="MyProject.View.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:vm="clr-namespace:MyProject.ViewModel;assembly=MyProject.ViewModel"
          Title="{Binding Path=Title}" Height="350" Width="525" DataContext="{Binding Source={StaticResource mainWindowViewModel}}"&gt;
      &lt;Window.Resources&gt;
          &lt;vm:MainWindow x:Key="mainWindowViewModel"/&gt;
      &lt;/Window.Resources&gt;
  
  ...
  
  &lt;/Window&gt;
  </code></pre>
  
  <p>When I try to run this, I get the following exception "Provide value on 'System.Windows.StaticResourceExtension' threw an exception.  The line number and position point to the DataContext property, and the inner exception states "Cannot find resource named mainWindowViewModel.</p>
  
  <p>Below is the code for the View Model:</p>
  
  <pre><code>namespace MyProject.ViewModel
  {
      public class MainWindow : INotifyPropertyChanged
      {
          #region Fields
  
          private const string TitlebarPrefixString = "My Project";
          private string title = TitlebarPrefixString;
  
          public string Title {
              get
              {
                  return this.title;
              } // End getter
              set
              {    
                  this.title = value;
                  OnPropertyChanged("Title");
              } // End setter
          } // End property
  
          protected virtual void OnPropertyChanged(string propertyName)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (handler != null)
              {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              } // End if
          } // End method
  
  
          public event PropertyChangedEventHandler PropertyChanged;
  
      } // End class
  } // End namespace
  </code></pre>
  
  <p>My theory is that the resources are loaded after the attempt to bind the title to the property.  When the exception is thrown, the Resources property for the Window is empty.</p>
  
  <p>Is the only solution to set the DataContext in the Code Behind?  I can get this to work, but I would prefer to keep it in XAML.</p>