[英]Populating listbox from csv via list<>
就像標題所暗示的那樣,我正在嘗試使用來自csv的信息填充WPF應用程序中的列表框。 理想情況下,我想顯示csv中的每一行,但僅顯示某些列,例如具有適應性數據的名字,姓氏,DOB,性別等。但是我只顯示名字和姓氏。 我的代碼如下:
public partial class PatientList : Window
{
int x = 0;
public PatientList()
{
InitializeComponent();
HumanResources ListAllPatients = new HumanResources();
List<PatientInformation> AllPatients = ListAllPatients.PopPatientList();
foreach (PatientInformation PatientChoice in AllPatients)
{
lboxPatients.Items.Add(AllPatients[x]);
x += 1;
}
}
人力資源課
public List<PatientInformation> PopPatientList()
{
DataAccess PatientList4Doc = new DataAccess();
List<PatientInformation> DocsPatients = PatientList4Doc.ListofPatients();
return DocsPatients;
}
資料存取類別
class DataAccess
{
public List<PatientInformation> ListofPatients()
{
List<PatientInformation> ListofPatients = new List<PatientInformation>();
string[] UserData = File.ReadAllLines("UserList.csv");
foreach (string Person in UserData)
{
string[] PersonInfo = Person.Split(',');
PatientInformation Patient = new PatientInformation()
{
Username = PersonInfo[0].ToUpper(),
LastName = PersonInfo[1],
FirstName = PersonInfo[2],
DOB = Convert.ToDateTime(PersonInfo[3]),
Gender = Convert.ToChar(PersonInfo[4]),
Height = Convert.ToInt16(PersonInfo[5]),
Weight = Convert.ToInt16(PersonInfo[6]),
CaloricIntake = PersonInfo[7],
WaterDrank = PersonInfo[8],
CaloriesBurned = PersonInfo[9],
TimeSlept = PersonInfo[10],
ContextMessage = PersonInfo[11],
Replymessage = PersonInfo[12]
};
ListofPatients.Add(Patient);
}
return ListofPatients;
}
患者信息課
public class PatientInformation
{
public string Username { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public DateTime DOB { get; set; }
public char Gender { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
public string CaloricIntake { get; set; }
public string WaterDrank { get; set; }
public string CaloriesBurned { get; set; }
public string TimeSlept { get; set; }
public string ContextMessage { get; set; }
public string Replymessage { get; set; }
}
一切正常,直到我嘗試填充列表框。 當我嘗試讀取信息時,我得到的只是代碼路徑而不是所需的信息。 我的問題是:從CSV提取所有信息后,如何將其打印到列表框中並顯示PatientInformation中的內容? 預先感謝您的任何幫助
編輯
@Hypnos:我這樣實現了您的建議:
I tried implementing your example like so: HumanResources ListAllPatients = new HumanResources();
List<PatientInformation> AllPatients = ListAllPatients.PopPatientList();
List<PatientInformation> p = new List<PatientInformation>();
p.Add(new PatientInformation() { Username = Convert.ToString(AllPatients[0]), FirstName = Convert.ToString(AllPatients[2]), LastName = Convert.ToString(AllPatients[1]) });
lboxPatients.ItemsSource = p;
但是,這將返回異常“索引超出范圍。必須為非負數並且小於集合的大小”
我認為您應該自定義ListItem DataTemplate。 看那個例子:
在后面的代碼中,出於實際原因,我創建了一個新列表,並在其中添加了一個條目
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<PatientInformation> p = new List<PatientInformation>();
p.Add(new PatientInformation() { Username = "John", FirstName ="John", LastName = "Doe" });
listBox.ItemsSource = p;
}
在XAML方面,要查看特定字段,我們可以做類似的事情:
<ListBox x:Name="listBox" HorizontalAlignment="Left" Height="291" VerticalAlignment="Top" Width="497" Margin="10,10,0,0" DataContext="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如您所見,除了在ListBox本身上的DataContext = {Binding}外,我還為ListItem創建了一個自定義的DataTemplate,以便將綁定重定向到它希望在控件上的屬性。 以我為例,兩個TextBlocks將公開FirstName和LastName屬性。
希望這可以幫助
ListBox沒有列的概念,因此我想您要在其中顯示簡單的字符串行。 然后,您可以簡單地將字符串添加到Items集合中:
public PatientList()
{
InitializeComponent();
HumanResources ListAllPatients = new HumanResources();
List<PatientInformation> AllPatients = ListAllPatients.PopPatientList();
foreach (PatientInformation PatientChoice in AllPatients)
{
lboxPatients.Items.Add(string.Format("{0};{1}", PatientChoice.FirstName, PatientChoice.LastName));
}
}
如果要顯示帶有標題的實際列,則可以使用ListView:
<ListView x:Name="lv">
<ListView.View>
<GridView>
<GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" />
<GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
</GridView>
</ListView.View>
</ListView>
public PatientList()
{
InitializeComponent();
HumanResources ListAllPatients = new HumanResources();
List<PatientInformation> AllPatients = ListAllPatients.PopPatientList();
lv.ItemsSource = AllPatients;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.