Observable sub 属性 字符串到列表框绑定

Observable sub property String to listbox binding

抱歉,这个问题可能很简单。

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

我的视图模型中有一个可观察的集合,但是每当我将任何属性绑定到列表框时,它returns第一个条目的每个字符都有自己的行

抱歉,我手头没有代码,但总结一下

с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 来查询它 returns 正确的结果,但是由于实际实现有多个字段,这将是一种繁琐的解决方法。有什么想法吗?

ListBox itemsSource 应该是 ListOfAppointments,您可以在数据模板中绑定主题 属性

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

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

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

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