wpf数据模板
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<!--定义数据模板内容-->
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!-- 使用Binding绑定数据 -->
<Border Width="10" Height="10" Background="{Binding Code}"/>
<TextBlock Margin="10,0" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Color> test = new List<Color>();
test.Add(new Color() { Code = "#ff0000", Mame = "红色" });
test.Add(new Color() { Code = "#00ff00", Mame = "绿色" });
test.Add(new Color() { Code = "#0000ff", Mame = "蓝色" });
list.ItemsSource = test;
}
}
public class Color
{
public string Code { get; set; }
public string Mame { get; set; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
上次更新: 2023/12/08, 20:54:32