Create aclass called Person
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
Then, in xaml,
<ListBox x:Name="lbPeople" Grid.Row="0" Grid.Column="0" Height="250" Width="300" HorizontalAlignment="Left"
ScrollViewer.HorizontalScrollBarVisibility="Hidden" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="10" Text="{Binding FirstName}" />
<TextBlock Grid.Column="1" Margin="10" Text="{Binding LastName}" />
<TextBlock Grid.Column="2" Margin="10" Text="{Binding Age}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In C# code,
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
lbPeople.ItemsSource = GetPeople();
}
public List<Person> GetPeople()
{
string[] firstNames = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" };
string[] lastNames = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" };
List<Person> people = new List<Person>();
Random r2 = new Random();
Random r = new Random();
for (int i = 0; i < 10; i++)
{
int index = r.Next(0, 11);
Person p = new Person
{
FirstName = firstNames[index],
LastName = lastNames[index],
Age = r2.Next(10, 90)
};
people.Add(p);
}
return people;
}
No comments:
Post a Comment