通过XAML绑定将自定义对象传递给UserControl

我要做的是创建一个UserControl,我可以传递一个Address对象。 似乎当我将Address="{Binding Path=Person.Address}"传递给UserControl时,嵌入的TextBox绑定到Text="{Binding Path=Person.Address}"而不是Text="{Binding Path=Address.Summary}"

我错了吗?

如果您想使用该项目,可以使用以下链接: http : //dl.dropbox.com/u/4220513/WpfApplication2.zip

域对象:

 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); } } } } 

主窗口:

 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)); } } } }         

用户控件:

 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)); } }    

错误:

 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') 

在MainWindow.xaml中:

  

然后在AddressView.xaml中

  

这会显示我的摘要。