【发布时间】:2017-08-09 07:30:30
【问题描述】:
因此,在我的 C# (WPF) 应用程序中,我使用一个表单来填充患者列表。我需要这些患者在添加时显示在列表视图中。
public class Patients
{
public string lastname;
public string firstname;
public string rm;
public int age;
public string notes;
public int status;
public Patients(string lastname, string firstname, int age, string rm, string notes, int status)
{
this.lastname = lastname;
this.firstname = firstname;
this.notes = notes;
this.status = status;
}
}
public partial class MainWindow : Window
{
public List<Patients> newPatientList = new List<Patients>();
public void AddNewPatient(string lastname, string firstname, int age, string rm, string notes, int status)
{
newPatientList.Add(new Patients(lastname, firstname, age, rm, notes, status));
}
}
这会将患者正常添加到列表中。
<ListView ItemsSource="{Binding newPatientList}" x:Name="listView" HorizontalAlignment="Stretch" Margin="0,0,0,0" SelectionChanged="listView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="RM #" DisplayMemberBinding="{Binding rm}"/>
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding lastname}"/>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding firstname}"/>
<GridViewColumn Header="Status" DisplayMemberBinding="{Binding status}"/>
</GridView>
</ListView.View>
</ListView>
我正在尝试将数据绑定到列表,但它没有填充。
【问题讨论】:
-
这是因为
List<T>没有通知绑定控件其集合已更改。尝试使用ObservableCollection<T>而不是List<T> -
... 和一个 ViewModel 而不是将模型填充到 View 代码中。
-
离题了,当这个类的一个实例代表一个单个患者时,调用你的类
Patients是很奇怪的。我有一个偷偷摸摸的怀疑你使用复数是因为List<Patients>,但你最好使用List<Patient>。尽管在处理列表时听起来不太正确,但在处理类不包含在列表中时,使用单数作为类名听起来会更正确。您已经可以在示例中看到它发生了,AddNewPatient()(单数)方法执行以下操作:.Add(new Patients())(复数)