简体   繁体   English

如何从Contact类绑定属性?

[英]How to bind property from Contact class?

I have a class with a property named "ContactInfo" of type Microsoft.Phone.UserData.Contact. 我有一个名为Microsoft.Phone.UserData.Contact类型的名为“ ContactInfo”的属性的类。 I read the contacts from the phone and for each contact I create an object of my class and set the property ContactInfo. 我从电话中读取联系人,并为每个联系人创建类的对象并设置属性ContactInfo。 All the objects are added to an ObservableCollection that is shown in a LongListSelector. 将所有对象添加到LongListSelector中显示的ObservableCollection中。

In C# i can read the elements from the datasource of the ObservableCollection and read the properties from the ContactInfo property of each object but in XAML the data template can't access the properties. 在C#中,我可以从ObservableCollection的数据源读取元素,并从每个对象的ContactInfo属性读取属性,但是在XAML中,数据模板无法访问属性。

I know that the data source for the LongListSelector is set correctly because removing the data template it shows the object.toString() but setting the data template nothing is shown and in the outpout tab of VS I get the message "System.Windows.Data Error: BindingExpression path error: 'DisplayName' property not found on 'ContactInfo' 我知道LongListSelector的数据源设置正确,因为删除数据模板会显示object.toString(),但未设置数据模板会显示任何内容,在VS的outpout选项卡中,我收到消息“ System.Windows.Data”错误:BindingExpression路径错误:在“ ContactInfo”上找不到“ DisplayName”属性

Am I forgetting something or is there something set incorrectly? 我是忘记了还是设置不正确?

This is my model: 这是我的模型:

public class CDContact
{
    public Contact ContactInfo { get; set; }

    public ObservableCollection<CDPhoneNumber> PhoneNumbers{get; set;}

    public CDContact()
    {
        PhoneNumbers = new ObservableCollection<CDPhoneNumber>();
    }
}

This is the data template: 这是数据模板:

<DataTemplate x:Key="ContactItemTemplate">
    <StackPanel VerticalAlignment="Top" DataContext="ContactInfo" >
            <TextBlock FontWeight="Bold" Text="{Binding Path=DisplayName, Mode=OneWay}" />
    </StackPanel>
</DataTemplate>

Assuming everything else is correctly setup, this should work: 假设其他所有设置均正确,则该方法应该有效:

<DataTemplate x:Key="ContactItemTemplate">
    <StackPanel VerticalAlignment="Top" >
        <TextBlock FontWeight="Bold" Text="{Binding Path=ContactInfo.DisplayName, Mode=OneWay}" />
    </StackPanel>
</DataTemplate>

This part seems wrong : 这部分似乎是错误的:

<StackPanel VerticalAlignment="Top" DataContext="ContactInfo" >

Try it this way instead : 尝试这种方式:

<StackPanel VerticalAlignment="Top" DataContext="{Binding ContactInfo}" >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM