2017-08-27 81 views
0

如果我使用以下代码来显示ListBox中的对象,如何在TextBlock中显示它?TextBlock显示对象列表

listStudents.Items.Clear(); 
foreach (Student sRef in StudentList) 
{ 
    listStudents.Items.Add(sRef); 
} 
+0

更改ListBox上的数据模板 – kurakura88

回答

1

,你可以追加到TextBlockText属性:

foreach (Student sRef in StudentList) 
{ 
    textBlock1.Text += sRef.Firstname + " " sRef.Lastname + Environment.NewLine; 
} 

但是您可能想定义一个定义每个对象在视图中的外观的ItemTemplate

<ListBox x:Name="listStudents"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Firstname}" /> 
       <TextBlock Text="{Binding Lastname}" Margin="2 0 0 0" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
0
listStudent.Items.Clear(); 
     foreach (Student sRef in StudentList) 
     { 
      listSubjects.Items.Add(sRef.firstname + " " + sRef.lastname); 
     } 
0

如果你只想显示没有选择和其他ListBox功能对象的列表,这是方便使用ItemsControl - 它有ItemTemplate属性来控制每个项目和其他有用的东西的外观(实际上,ListBox继承ItemsControl)。只需使用ItemsControlitemsStudent变量,并使用相同的代码:如果你想在一个TextBlock显示学生的几行

itemsStudent.Items.Clear(); 
foreach (Student sRef in StudentList) 
{ 
    itemsStudent.Items.Add(sRef); 
}