因此,在我的C#(WPF)应用程序中,我使用表单来填充患者列表。我需要这些患者在列表视图中显示,因为他们被添加。C#从列表中填充列表视图
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'不会通知绑定控件它的集合已经改变。尝试使用'ObservableCollection '而不是'List ' –
...和ViewModel,而不是将模型填充到视图代码中。 – Fildor
Offtopic,当这个班级的一个实例代表一个_single_患者时,调用你的班级“患者”是非常奇怪的。我有一个鬼鬼祟祟的嫌疑,你已经使用了复数,因为'List',但你最好使用'List '。尽管在处理列表时,听起来不那么正确,但在处理类时,使用单数作为类名听起来会更加正确 - 当它不是列在list_中时。你已经可以看到它发生在你的例子中,方法'AddNewPatient()'(奇异)会执行以下操作:'.Add(new Patients())'(复数) –
Flater