在ListBox中显示对象属性

时间:2015-03-16 09:45:01

标签: c# wpf

我想显示该物业" Title"从ListBox中的对象列表:

<ListBox ItemsSource="{Binding SelectableSurveysByYear}"
         DisplayMemberPath="{Binding Title}" 
         SelectedItem="{Binding SelectedSurvey}">

然而,我看到的不是标题,而是我班级的名称,三次。 SelectableSurveysByYear是一个ObservableCollection of Surveys:

public class Survey
{
    public string Title { get; set; }
}

我的缺陷在哪里?

2 个答案:

答案 0 :(得分:2)

DisplayMemberPath属性不支持Binding语法。请尝试以下方法:

<ListBox ItemsSource="{Binding SelectableSurveysByYear}"
     DisplayMemberPath="Title" 
     SelectedItem="{Binding SelectedSurvey}">

答案 1 :(得分:0)

哦......这个缺陷是在定义DisplayMemberPath时。这有效:

   <ListBox ItemsSource="{Binding SelectableSurveysByYear}"
         DisplayMemberPath="Title" 
         SelectedItem="{Binding SelectedSurvey}">
相关问题