title: WPF bind title of window to property
body: <p>I am trying to bind the value of a property (MyTitle) of a class (MainWindow) that derives from Window. I have created a dependency property called MyTitleProperty, implemented the INotifyPropertyChanged interface and modified the set method of MyTitle to call the PropertyChanged event, passing "MyTitle" as the property name parameters. I set MyTitle to "Title" in the constructor but when the window opens the title is blank. If I put a break point on the Loaded event then MyTitle = "Title" but this.Title = "". This is surely something unbelievably obvious that I've not noticed. Please help!</p>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window
      x:Class="WindowTitleBindingTest.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:this="clr-namespace:WindowTitleBindingTest"
      Height="350"
      Width="525"
      Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}}"
      Loaded="Window_Loaded"&gt;
      &lt;Grid&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow.xaml.cs:</p>
  
  <pre><code>public partial class MainWindow : Window, INotifyPropertyChanged
  {
      public static readonly DependencyProperty MyTitleProperty = DependencyProperty.Register("MyTitle", typeof(String), typeof(MainWindow));
  
      public String MyTitle
      {
          get { return (String)GetValue(MainWindow.MyTitleProperty); }
          set
          {
              SetValue(MainWindow.MyTitleProperty, value);
              OnPropertyChanged("MyTitle");
          }
      }
  
      public MainWindow()
      {
          InitializeComponent();
  
          MyTitle = "Title";
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(String propertyName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
      }
  }
  </code></pre>