First of all, remove DataContext="MainWindow" , since this sets the DataContext from Window to the string MainWindow, then you specify the ElementName for your binding, which defines the 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 assigning the DataContext , which is the default source, if none are specified, from Window to TheBook
public SimpleBinding() { ... this.DataContext = TheBook; }
or by specifying the RelativeSource your Window binding, which TheBook provides:
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=TheBook.BookName}"/>
but since you cannot snap to fields, you need to convert TheBook into a property:
public partial class SimpleBinding : Window { public Book TheBook { get; set; } ... }
dkozl source share