title: Passing custom objects to UserControl via XAML binding
body: <p>What I'm trying to do is create a UserControl to which I can pass an Address object.  It seems that when I pass <code>Address="{Binding Path=Person.Address}"</code> to the UserControl, the embedded TextBox is binding to <code>Text="{Binding Path=Person.Address}"</code> instead of <code>Text="{Binding Path=Address.Summary}"</code></p>
  
  <p>Am I going about this all wrong?</p>
  
  <p>Here's a link to the project if you want to play with it: <a href="http://dl.dropbox.com/u/4220513/WpfApplication2.zip" rel="nofollow">http://dl.dropbox.com/u/4220513/WpfApplication2.zip</a></p>
  
  <p>Domain objects:</p>
  
  <pre><code>namespace WpfApplication2
  {
      public class Person
      {
          public String Name { get; set; }
          public Address Address { get; set; }
      }
  
      public class Address
      {
          public String Street { get; set; }
          public String City { get; set; }
  
          public String Summary { get { return String.Format("{0}, {1}", Street, City); } }
      }
  }
  </code></pre>
  
  <p>MainWindow:</p>
  
  <pre><code>namespace WpfApplication2
  {
      public partial class MainWindow : Window
      {
          private readonly ViewModel vm;
          public MainWindow()
          {
              InitializeComponent();
              vm = new ViewModel();
              DataContext = vm;
  
              vm.Person = new Person()
              {
                  Name = "Bob",
                  Address = new Address()
                  {
                      Street = "123 Main Street",
                      City = "Toronto",
                  },
              };
          }
      }
  
      public class ViewModel : INotifyPropertyChanged
      {
          private Person person;
          public Person Person { get { return person; } set { person = value; NotifyPropertyChanged("Person"); } }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected void NotifyPropertyChanged(String propertyName)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
              }
          }
      }
  }
  
  &lt;Window x:Class="WpfApplication2.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:WpfApplication2"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;StackPanel&gt;
          &lt;TextBlock Text="Name:" /&gt;
          &lt;TextBlock Text="{Binding Path=Person.Name}" /&gt;
          &lt;TextBlock Text="Address:" /&gt;
          &lt;local:AddressView Address="{Binding Path=Person.Address}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>UserControl:</p>
  
  <pre><code>namespace WpfApplication2
  {
      public partial class AddressView : UserControl
      {
          public AddressView()
          {
              InitializeComponent();
              DataContext = this;
          }
  
          public Address Address
          {
              get { return (Address)GetValue(AddressProperty); }
              set { SetValue(AddressProperty, value); }
          }
  
          public static readonly DependencyProperty AddressProperty =
              DependencyProperty.Register("Address", typeof(Address), typeof(AddressView));
      }
  }
  
  &lt;UserControl x:Class="WpfApplication2.AddressView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;TextBox Text="{Binding Path=Address.Summary}" IsReadOnly="True" /&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>System.Windows.Data Error: 40 : BindingExpression path error: 'Person' property not found on 'object' ''AddressView' (Name='')'. BindingExpression:Path=Person.Address; DataItem='AddressView' (Name=''); target element is 'AddressView' (Name=''); target property is 'Address' (type 'Address')
  </code></pre>