WPF基础入门 Class3:WPF数据模板 先在cs文件中定义一些数据 public partial class Class_4 : Window { public Class_4() {
先在cs文件中定义一些数据
public partial class Class_4 : Window { public Class_4() { InitializeComponent(); List<Color> test = new List<Color>(); test.Add(new Color() { Code = "Yellow", Name = "Red" }); test.Add(new Color() { Code = "#00FF00", Name = "Green" }); test.Add(new Color() { Code = "#0000FF", Name = "Blue" }); //数据绑定到list list.ItemsSource = test; } } public class Color { public string Code { get; set;} public string Name { get; set; } }
xaml中编写模板
<Grid> <ListBox x:Name="list"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <BorderWidth="10"Height="10"Background="{Binding Code}">Border> <TextBlock Margin="10, 0" Text="{Binding Name}">TextBlock> StackPanel> DataTemplate> ListBox.ItemTemplate> ListBox>Grid>
效果:
来源地址:https://blog.csdn.net/qq_43622870/article/details/132501555
--结束END--
本文标题: WPF基础入门-Class3-WPF数据模板
本文链接: https://lsjlt.com/news/382650.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0