上善若水

水善利万物而不争
随笔 - 175, 文章 - 0, 评论 - 10, 阅读 - 14万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF | 跟着视频敲代码5 | 数据模板 DataTemplate

Posted on   董锡振  阅读(35)  评论(0编辑  收藏  举报

视频链接:https://www.bilibili.com/video/BV1nY411a7T8?p=4

笔记:

DataTemplate:数据模板 

复制代码
 <ListBox x:Name="list" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border Width="10" Height="10" Background="Red"></Border>
                        <TextBlock Margin="10,0" Text="Red"></TextBlock>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>        List<int> test = new List<int>();
复制代码
1
2
3
4
5
<em id="__mceDel">            for (var i = 0; i < 10; i++)
            {
                test.Add(i);
            }
            list.ItemsSource = test;<br><br><br>或者:前端使用Binding连接<br><br></em>

public class Color
{
public string Code { get; set; }
public string Name { get; set; }
}

 

List<Color> test = new List<Color>();
test.Add(new Color() { Code = "#FFB6C1", Name = "粉红色" });
test.Add(new Color() { Code = "#FF0000", Name = "红色" });
list.ItemsSource = test;

 

<ListBox x:Name="list" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="10" Height="10" Background="{Binding Code}"></Border>
<TextBlock Margin="10,0" Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

 

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示