你能解释为什么Binding不适用于ItemsControl的DisplayMemberPath吗?

时间:2013-08-07 16:41:54

标签: wpf silverlight dependency-properties itemscontrol

任何人都可以解释为什么Binding不能用于ItemsControl的DisplaymemberPath吗?

我用Reflector DisplayMemberPath检查ItemsControl只有Dependency Property,Bindable属性也只设置为True。

XAML:

  <ComboBox x:Name="display" DisplayMemberPath="{Binding NewAddress.TelePhone}" ItemsSource="{Binding Persons}"/>

人员类:

public class Person
{
    public Person()
    {
        persons = new ObservableCollection<NewAddress>();
        persons.Add(new NewAddress() { TelePhone = "MyNo" });
        persons.Add(new NewAddress() { TelePhone = "MyNo1" });
        persons.Add(new NewAddress() { TelePhone = "MyNo2" });
        persons.Add(new NewAddress() { TelePhone = "MyNo3" });
    }

    private ObservableCollection<NewAddress> persons;

    public ObservableCollection<NewAddress> Persons
    {
        get { return persons; }
        set { persons = value; }
    }

}

地址类:

 public class NewAddress
{
    public string  TelePhone { get; set; }
}

1 个答案:

答案 0 :(得分:4)

DisplayMemberPath是属性的实际名称,而不是对属性的绑定。将您的XAML代码更改为以下内容:

<ComboBox x:Name="display" DisplayMemberPath="TelePhone" ItemsSource="{Binding Persons}"/>