【发布时间】:2013-12-17 19:10:19
【问题描述】:
我希望名为 LstbClients 的 ListBox 在每个 Label 或 TextBlock 中显示 Name 和 Phonenumber(这对我来说无关紧要),我之前使用 ComboBox 进行了 DataBinded,效果很好,但由于某种原因,它不适用于此列表框。
这是 XAML 代码。
<ListBox x:Name="lstbClients"
Height="300"
Grid.Row="0" Grid.Column="0"
Style="{StaticResource inputControls}"
ItemsSource="{Binding clients}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}"/>
<Label Content=", "/>
<Label Content="{Binding Phonenumber}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是后面的代码:
//clients is a filled ObservableCollection<client>
lstbClients.ItemsSource = clients;
这是 Client.cs 背后的代码
public class Client
{
public int ID;
public string Lastname;
public string Name;
public string Streetname;
public string Postcode;
public string City;
public int Phonenumber;
public string Email;
public DateTime CreationDate;
public DateTime BirthDate;
public Client(int id, string lastname, string name, DateTime birthDate, string streetname, string postcode, string city, int phonenumber, string email, DateTime creationDate)
{
this.ID = id;
this.Lastname = lastname;
this.Name = name;
this.Streetname = streetname;
this.Postcode = postcode;
this.City = city;
this.Phonenumber = phonenumber;
this.Email = email;
this.CreationDate = creationDate;
this.BirthDate = birthDate;
}
}
由于某些奇怪的原因,ListBox 的标签只显示“,”并忽略名称和电话号码,是的,当我用我的最终 WPF 应用程序“打开”ListBox 时,所有数据确实包含......所以ListBox 获取的是数据,但它只是不想在 lstbClients 的标签上显示它,所以我无法识别哪个标签包含哪些数据。
【问题讨论】:
-
显示客户端类的代码。为什么要从 XAML 和背后的代码设置 ItemsSource?
-
欢迎来到本站!没有必要在你的问题中加上“对不起,我是初学者”——每个人都是初学者,所以不用担心。祝你的项目好运!
-
您的 XAML 看起来不错,我猜您尚未在 Observable 集合上实现
INotifyPropertyChanged。请显示 Observable Collection 的声明代码。此外,在运行时检查您的输出窗口,以确保您没有收到绑定错误。这些可以帮助您找到问题。 -
我确实没有 INotifyPropertyChanged... 这不是 MVVM 的东西吗?
标签: c# wpf xaml mvvm itemssource