C# Caliburn将ListBox微绑定到集合中对象的属性
我在将列表框的ItemsSource绑定到对象集合,然后将这些对象的属性显示为列表项时遇到问题 我的XAML代码:C# Caliburn将ListBox微绑定到集合中对象的属性,c#,wpf,caliburn.micro,C#,Wpf,Caliburn.micro,我在将列表框的ItemsSource绑定到对象集合,然后将这些对象的属性显示为列表项时遇到问题 我的XAML代码: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/ble
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="CaliburnMicroBasic.ShellView"
d:DesignWidth="358" d:DesignHeight="351">
<Grid Width="300" Height="300" Background="LightBlue">
<ListBox ItemsSource="{Binding ListOfPeople}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding PersonName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
我的ViewModel:
namespace CaliburnMicroBasic {
using Caliburn.Micro;
using System.Collections.ObjectModel;
using System.Windows;
public class ShellViewModel : Screen, IShell
{
public Person SelectedPerson{ get; private set; }
public ObservableCollection<Person> ListOfPeople{ get; private set; }
public ShellViewModel()
{
ListOfPeople = new ObservableCollection<Person>();
ListOfPeople.Add(new Person("Name 1"));
ListOfPeople.Add(new Person("Name 2"));
ListOfPeople.Add(new Person("Name 3"));
ListOfPeople.Add(new Person("Name 4"));
}
}
public class Person
{
public string PersonName { get; private set; }
public Person(string personName)
{
_personName = personName;
}
}
}
namespace-CaliburnMicroBasic{
使用Caliburn.Micro;
使用System.Collections.ObjectModel;
使用System.Windows;
公共类ShellViewModel:屏幕,IShell
{
public Person SelectedPerson{get;private set;}
公共ObservableCollection人员列表{get;private set;}
公共ShellViewModel()
{
ListOfPeople=新的ObservableCollection();
人员列表。添加(新人员(“姓名1”);
人员列表。添加(新人员(“姓名2”);
人员列表。添加(新人员(“姓名3”);
人员列表。添加(新人员(“姓名4”);
}
}
公共阶层人士
{
公共字符串PersonName{get;private set;}
公众人物(字符串personName)
{
_personName=personName;
}
}
}
如您所见,我试图让listbox使用Person.PersonName作为listbox中每个文本块的内容,但我得到的只是listbox中的四个空行。换句话说,列表框包含正确数量的项目,但没有一个项目被正确呈现
有人能看出我做错了什么吗?您从未将任何内容分配给PersonName属性。将代码更改为:
public Person(string personName)
{
this.PersonName = personName;
}
并删除您的私有字段。在这些情况下获取更多数据的提示:检查调试输出,看看是否有类似“找不到引用绑定的源…”这样的行。他们可以帮你找到问题。您还可以在数据绑定中将调试跟踪级别设置为高,以获取更多信息。看见