2016-12-06 5 views
0

タイトルのように、WPFアプリケーションのリストボックスにCSVの情報を入力しようとしています。理想的には、CSVのすべての行を表示したいが、特定の列、つまりフィットネスデータがある場合は、名字、DOB、性別などが表示されますが、姓と名のみが表示されます。次のように私のコードは次のとおりです。リストから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のものをどのように表示できますか?任意の助けを事前に感謝

This is what I'm getting

EDIT

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

しかし、これは「インデックスのうち、例外で返します。私はそうのようなあなたの提案を実装範囲は負でなく、コレクションのサイズより小さくなければなりません "

答えて

0

リストボックスに列の概念がないのに役立ちます。あなたは、単にItemsコレクションに文字列を追加することができます。

 <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; 
    } 
:あなたは、リストビューを使用することができ、ヘッダーと実際の列を表示したい場合は

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

0

私はあなたの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 =はlistBox自体に、私は、リストアイテム(複数可)用にカスタマイズするDataTemplateを作成し、そのプロパティへの結合をリダイレクトするためにきた{バインディング}私が望むコントロールに 私の場合は、FirstNameプロパティとLastNameプロパティを公開する2つのTextBlocksがあります。

ホープこれは、私はあなたがそれで文字列のシンプルなラインを表示したいと思いますので、

関連する問題