2015-04-23 49 views
0

对不起,可能是一个简单的问题。Observable子属性String to listbox binding

我有一个类具有各种字符串属性的约会。

我有他们的观察集合在我的视图模型,但是每当我绑定到任何属性的列表框它与每一个字符有它自己的行的第一个条目返回

对不起,我不手头上的代码,但概括起来

сlass Appointment 
{ 
    public Appointment(string ыubject) 
    { 
     Subject = subject; 
    } 

    public string Subject { get; set; } 
} 

class Appointments 
{ 
    public Appointments() 
    { 
     ListOfAppointments = new ObservableCollection<Appointment>(); 
     ListOfAppointments.Add(new ListOfAppointments("Example")); 
    } 

    public ObservableCollection<Appointment> ListOfAppointments { get; set; } 
} 

在XAML:

<ListBox DataContext="{StaticResource AppointementViewModel, Path=ListOfAppointments}" ItemSource="{Binding Subject }" /> 

希望这是正确的。列表框中显示的结果是

E 
X 
A 
M 
P 
L 
E 

我敢线索少的时刻,因为我不是初始后更新信息获取我没有INotifyProperty实施

请注意数据被加载正确地说,这只是一个复制的简单例子。

另外,如果我在后端使用linq来查询它,它会返回正确的结果,但实际实现有多个字段,这将是一个繁琐的解决方法。有任何想法吗?

回答

4

列表框的ItemsSource应ListOfAppointments并在DataTemplate中,你可以将有关物业

<ListBox DataContext="{StaticResource AppointementViewModel}" ItemSource="{Binding ListOfAppointments}" ItemTemplate="{StaticResource MyTemplate}"/> 

<DataTemplate x:Key="MyTemplate"> 
    <TextBlock Text="{Binding Subject}" /> 
</DataTemplate> 

在这个问题给出的示例代码绑定,ListOfAppoinments给出的ListBoxDataContextSubject作为ItemsSource 。因此,ListBox将ListOfAppoinments中第一个对象的Subject属性作为其ItemsSource。所以字符串被拆分并分配给每个ListBoxItem

在上面的代码中,我们给出ListOfAppoinments作为ItemsSource。因此,每个Appointment对象都从列表中取出并分配给ListBoxItems。由于我们已将Subject绑定到DataTemplate中的TextBlock,因此将显示该列表。

+0

谢谢你的工作,明天将实施此修复程序,但我想知道为什么它将字符串分解为字符?然而,如果我使用LINQ在后端属性中选择主题,它最终会完美地绑定。再次感谢您的帮助。 – user2008572