通过列表<>从csv填充列表框

时间:2016-12-06 10:41:14

标签: c# wpf csv listbox

与标题建议一样,我正在尝试使用来自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的内容?在此先感谢您的任何帮助

This is what I'm getting

修改

@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;

但是,这会返回一个异常“索引超出范围。必须是非负数且小于集合的大小”

2 个答案:

答案 0 :(得分:0)

我认为您应该自定义ListItem DataTemplate。 看看那个样本:

在代码背后,出于实际原因,我创建了一个新的List,在其中添加了一个条目

    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属性。

希望这有帮助

答案 1 :(得分:0)

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;
    }