方法一:使用 DisplayMemberPath 和复合属性
实例类:
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string DisplayName
{
get { return Name + Age.ToString(); }
set { DisplayName = value; }
}
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="9*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Grid.Row="0" x:Name="stdListBox" DisplayMemberPath="DisplayName"></ListBox>
<Button Grid.Row="1" Click="Button_Click" Content="展示"></Button>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
List<Student> students = new List<Student>()
{
new Student(){ Name ="小红", Age = 15},
new Student(){ Name ="小榄", Age = 17},
new Student(){ Name ="小绿", Age = 18}
};
this.stdListBox.ItemsSource = students;
}

方法二:使用 ItemTemplate
<ListBox Grid.Row="0" x:Name="stdListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Name}"></Run>
<Run Text="{Binding Age}"></Run>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

7013

被折叠的 条评论
为什么被折叠?



