当前位置: 首页 > news >正文

c# wpf template itemtemplate+ListBox

1.概要

2.代码

<Window x:Class="WpfApp2.Window7"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfApp2"mc:Ignorable="d"Title="Window7" Name="win" Height="450" Width="800"><Window.Resources><DataTemplate x:Key="MyDataTemplate"><StackPanel Orientation="Horizontal"><Border Background="Pink"><TextBlock Text="{Binding Title}"/></Border><Button Content="{Binding Author}"  Cursor="Hand" Margin="10,0"/></StackPanel></DataTemplate></Window.Resources><Grid><ListBox Name="l1" Grid.Row="1" ItemsSource="{Binding BookList,ElementName=win}" ItemTemplate="{StaticResource MyDataTemplate}"/></Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;namespace WpfApp2
{/// <summary>/// Window7.xaml 的交互逻辑/// </summary>public partial class Window7 : Window{public Window7(){InitializeComponent();BookList.Add(new Book() { Title = "三国演义", Author = "罗贯中", Time = DateTime.Now.AddYears(-200) });BookList.Add(new Book() { Title = "红楼梦", Author = "曹雪芹", Time = DateTime.Now.AddYears(-150) });BookList.Add(new Book() { Title = "西游记", Author = "吴承恩", Time = DateTime.Now.AddYears(-230) });//l1.ItemsSource = BookList;}public List<Book> BookList { get; set; } = new List<Book>();}
}

 public class Book
 {
     public required string Title { get; set; }
     public required string Author { get; set; }
     public DateTime Time { get; set; }
 } 

3.运行结果

http://www.lryc.cn/news/334853.html

相关文章:

  • 关于JVM-三色标记算法剖析
  • 怎么看有没有装python
  • VS CODE环境安装和hello world
  • mysql性能索引调优易混点总结
  • 区块链与数字身份:探索Facebook的新尝试
  • 【pycharm】在debug循环时,如何快速debug到指定循环次数
  • 【蓝桥杯每日一题】4.8 公约数
  • 【MySQL学习】MySQL的慢查询日志和错误日志
  • # C++之functional库用法整理
  • 查看MySQL版本的方式
  • k8s_入门_命令详解
  • 腾讯、阿里、字节….等大厂都更喜欢什么样的简历?
  • OpenHarmony实战:帆移植案例(中)
  • 武汉星起航:创始人张振邦智慧领航,孵化伙伴共绘跨境新蓝图!
  • 上下收缩、折叠面板
  • XC7A35T-2FGG484 嵌入式FPGA现场可编程门阵列 Xilinx
  • 淘宝订单API接口:电商业务自动化的新选择
  • 识典百科词条创建技巧,教你如何轻松创建热门识典百科词条!
  • iOS 开发中上传 IPA 文件的方法(无需 Mac 电脑)
  • 2024-04-08 问AI: 介绍一下AI 大神 吴恩达
  • Leetcode面试经典150_Q12整数转罗马数字
  • Docker-compose部署Alertmanager+Dingtalk+Prometheus+Grafana实现钉钉报警
  • 算法刷题记录 Day40
  • Android JNI基础
  • 裙边挡边带是什么
  • chabot项目介绍
  • ChromeOS 中自启动 Fcitx5 和托盘 stalonetray
  • 画图理解JVM相关内容
  • Scikit-Learn K均值聚类
  • 蓝桥杯 - 受伤的皇后