1.通过List<string>进行赋值,没有字段绑定
// 前台xaml <ListBox x:Name="list1"> <ListBox.ItemTemplate> <DataTemplate DataType="ListBoxItem"> <Border BorderBrush="Red" BorderThickness="0,0,0,1"> <ContentControl Content="{Binding}"/> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> // 后台赋值 private void SetListBoxItemsSource() { List<string> list = new List<string>(); for (int i = 0; i < 10; i++) { list.Add("listboxitem" + i); } list1.ItemsSource = list; }
1.可用字段绑定
// 前台xaml <ListBox x:Name="list1"> <ListBox.ItemTemplate> <DataTemplate DataType="ListBoxItem"> <Border BorderBrush="Red" BorderThickness="0,0,0,1"> <ContentControl Content="{Binding name}"/> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> // 后台cs private void SetListBoxItemsSource() { List<Person> list = new List<Person>(); for (int i = 0; i < 10; i++) { list.Add(new Person() { name = "名字" + i, age = "2" + i }); } list1.ItemsSource = list; } // 类实体 class Person { public string name { get; set; } public string age { get; set; } }