从csv通过列表填充列表框<>
问题描述:
就像标题所暗示的,我试图用csv中的信息填充WPF应用程序中的列表框。理想情况下,我想显示csv中的每一行,但只显示某些列,例如健身数据,我会显示名字,姓氏,出生日期,性别等信息,但我只会显示姓氏和名字。我的代码如下:从csv通过列表填充列表框<>
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; }
}
一切工作正常,直到我尝试填充l istbox。当我尝试读取信息时,我所得到的只是代码路径而不是所需的信息。我的问题是:一旦将所有信息从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;
然而,这会返回一个异常“索引超出范围必须为非负值且小于集合的大小“
答
一个列表框没有列的概念,所以我想你想显示在它的字符串的简单的线条。然后,您可以简单地添加字符串项集合:
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;
}
答
我认为你应该定制你的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>
正如你可以看到,除了在DataContext = {结合}在列表框本身,我已经创建用于列表项(多个)定制的DataTemplate,以便重定向结合其特性,关于我想要的控制。 在我的情况下,两个TextBlocks将公开FirstName和LastName属性。
希望这有助于