WPF bind title of window to property

Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=Self}}"

public MainWindow()
{
    InitializeComponent();

    DataContext = this;

    MyTitle = "Title";
}

Then you just need in the XAML

Title="{Binding MyTitle}"

Then you don't need the dependency property.


First off, you don't need INotifyPropertyChanged if you just want to bind to a DependencyProperty. that would be redundant.

You don't need to set DataContext either, that's for a ViewModel scenario. (look into the MVVM pattern whenever you get a chance).

Now your declaration of dependency property is incorrect, it should be:

public string MyTitle
        {
            get { return (string)GetValue(MyTitleProperty); }
            set { SetValue(MyTitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyTitle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyTitleProperty =
            DependencyProperty.Register("MyTitle", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));

Notice the UIPropertyMetadata: it sets the default value for your DP.

And lastly, in your XAML:

<Window ...
       Title="{Binding MyTitle, RelativeSource={RelativeSource Mode=Self}}"
       ... />