First of all remove DataContext="MainWindow"
as this sets DataContext
of a Window
to a string
MainWindow, then you specify ElementName
for your binding which defines binding source as another control with x:Name="TheBook"
which does not exist in your Window
. You can make your code work by removing ElementName=TheBook
from your binding and either by assigning DataContext
, which is default source if none is specified, of a Window
to TheBook
public SimpleBinding()
{
...
this.DataContext = TheBook;
}
or by specifying RelativeSource
of your binding to the Window
which exposes TheBook
:
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=TheBook.BookName}"/>
but since you cannot bind to fields you will need to convert TheBook
into property:
public partial class SimpleBinding : Window
{
public Book TheBook { get; set; }
...
}