2015-04-23 48 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