因此,在我的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