2012-03-11 61 views
0

我有一个文件,其中第一行是电话,第二行是日期。 我读取文件并将内容添加到列表框中,所有内容都在不同的行中。 现在我改变了列表框以包含2个文本框。阅读文本文件并添加到列表框WP7

<ListBox Name="listBox1" ItemsSource="{Binding}"> 
    <DataTemplate> 
     <StackPanel> 
       <TextBlock Text="{Binding Phone}" FontSize="32"/> 
       <TextBlock Text="{Binding MyDate}" FontSize="16"/> 
     </StackPanel> 
    </DataTemplate> 

如何绑定从文件中的数据? 谢谢

回答

1

创建一个类来表示你的数据:

class PhoneDate 
{ 
public string Phone{get;set;} 
public DateTime MyDate{get;set;} 
} 

然后加载数据到一个列表

List<PhoneDate> data = //load 

最后将列表绑定到列表框中:

listBox1.ItemsSource = data; 

现在你的绑定将起作用。

+0

这是通过使用我如何读取文件 (StreamReader的读者=新的StreamReader(FILESTREAM)) { 做 { mPhone = reader.ReadLine(); MyDate = reader.ReadLine(); this.listBox1.Items.Add(mPhone); this.listBox1.Items.Add(MyDate); } while(!reader.EndOfStream); – 2012-03-11 15:55:20

+0

如何使用列表数据= //负荷 – 2012-03-11 15:56:39

+0

我能做到这一点,但列表为空 '代码' 使用(StreamReader的读者=新的StreamReader(FILESTREAM)){ 做 { mPhone = reader.ReadLine (); MyDate = reader.ReadLine(); list.Add(new PhoneDate(mPhone,MyDate)); } while(!reader.EndOfStream); listBox1.ItemsSource = list; } – 2012-03-11 17:19:03

0

如果你想让它们水平布局,那么你需要在StackPanel上使用Orientation =“Horizo​​ntal”。

如果你想标题和列排列,然后ListView中的GridView会做腋臭