C# Listbox不显示特定对象的值(数据绑定)
我希望名为LstbClients的列表框在每个标签或文本块中显示名称和电话号码(这对我来说并不重要),我以前使用组合框进行过数据索引,效果非常好,但由于某些原因,它不适用于此列表框 这是XAML代码C# Listbox不显示特定对象的值(数据绑定),c#,wpf,xaml,mvvm,itemssource,C#,Wpf,Xaml,Mvvm,Itemssource,我希望名为LstbClients的列表框在每个标签或文本块中显示名称和电话号码(这对我来说并不重要),我以前使用组合框进行过数据索引,效果非常好,但由于某些原因,它不适用于此列表框 这是XAML代码 <ListBox x:Name="lstbClients" Height="300" Grid.Row="0" Grid.Column="0" Style="{StaticResource inputControls}"
<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>
出于某种奇怪的原因,列表框的标签只显示“,”而忽略了名称和电话号码,是的,当我用我的最终WPF应用程序“打开”列表框时,所有数据都包含。。。因此ListBox获取数据,但它只是不想在lstbClient的标签上显示它,所以我无法识别哪个标签包含什么数据
绑定使用的是属性,而不是字段
将字段更改为如下属性:
public string Name {get; set;}
public int Phonenumber {get; set;}
另一方面,您应该为类实现
INotifyPropertyChanged
,以防在任何属性更改时刷新GUI。请参阅此以供参考-。显示客户端类的代码。为什么要从XAML和代码隐藏设置ItemsSource?欢迎访问该网站!没有必要在你的问题中加上“对不起,我是初学者”——每个人都是初学者,所以不用担心。祝你的项目好运!您的XAML看起来不错,我猜您没有在您的可观察集合上实现INotifyPropertyChanged
。请显示可观测集合声明的代码。另外,在运行时检查输出窗口,以确保不会出现绑定错误。这些可以帮助你找到你的问题。我确实没有改变InotifyProperty。。。这不是MVVM的东西吗?谢谢!这是可行的,我一直认为当你不把逻辑放进去的时候,get和set是不必要的。。。但看起来我错了。无论如何,感谢您的回答,我还将进一步了解InotifyPropertyChanged这些是自动属性。编译器将在后台自动为您创建备份字段。
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;
}
}
public string Name {get; set;}
public int Phonenumber {get; set;}