WPF基础入门
Class3:WPF数据模板
1、先在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; }
}
2、xaml中编写模板文章来源:https://www.toymoban.com/news/detail-674038.html
<Grid>
<!--WPF数据模板-->
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--通过Binding绑定了Background和Text 不用再业务代码cs中穿插控件操作-->
<Border
Width="10"
Height="10"
Background="{Binding Code}"></Border>
<TextBlock Margin="10, 0" Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
3、效果:
文章来源地址https://www.toymoban.com/news/detail-674038.html
到了这里,关于WPF基础入门-Class3-WPF数据模板的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!