<Grid> <ListBox x:Name="list"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Border Width="10" Height="10" Background="{Binding Code}"/> <TextBlock Text="{Binding Name}" Margin="10,0"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<MyColor> myColors = new List<MyColor>(); myColors.Add(new MyColor { Code = "Red", Name = "红色" }); myColors.Add(new MyColor { Code = "Green", Name = "绿色" }); myColors.Add(new MyColor { Code = "Blue", Name = "蓝色" }); list.ItemsSource= myColors; } } public class MyColor { public string Code { get; set; } public string Name { get; set; } }
标签:Code,Name,myColors,MyColor,new,DataTemplate,数据,public,模板 From: https://www.cnblogs.com/friend/p/16977954.html