c# wpf template itemtemplate+ListBox

news/2024/5/14 12:08:16

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.tangninghui.cn.cn/item-12631.htm

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

20240321-1-AB测试面试题

AB测试面试题 1. 介绍一下ABTest的步骤 ABtest就是为了测试和验证模型/项目的效果&#xff0c;在app/pc端设计出多个版本&#xff0c;在同一时间维度下&#xff0c;分别用组成相同/相似的群组去随机访问这些版本&#xff0c;记录下群组的用户体验数据和业务数据&#xff0c;最…

el-row一行放置3个el-col,有时出现空行现象

利用el-row的type属性&#xff0c;将type设置为flex&#xff0c;启用flex布局&#xff0c;再设置justify和align属性&#xff0c;即可避免该问题出现。 <el-row type"flex" class"row-bg"><el-col :span"6"><div class"gri…

postman接口自动化测试

Postman除了前面介绍的一些功能&#xff0c;还有其他一些小功能在日常接口测试或许用得上。今天&#xff0c;我们就来盘点一下&#xff0c;如下所示&#xff1a; 1.数据驱动 想要批量执行接口用例&#xff0c;我们一般会将对应的接口用例放在同一个Collection中&#xff0c;然…

【美团笔试题汇总】2023-09-02-美团春秋招笔试题-三语言题解(CPP/Python/Java)

&#x1f36d; 大家好这里是KK爱Coding &#xff0c;一枚热爱算法的程序员 ✨ 本系列打算持续跟新美团近期的春秋招笔试题汇总&#xff5e; &#x1f4bb; ACM银牌&#x1f948;| 多次AK大厂笔试 &#xff5c; 编程一对一辅导 &#x1f44f; 感谢大家的订阅➕ 和 喜欢&#x1f…

HarmonyOS 应用开发之跨应用数据共享

跨应用数据共享提供了向其他应用共享以及管理其数据的方法&#xff0c;支持不同应用之间的数据协同。 在许多应用场景中都需要用到数据共享&#xff0c;比如将电话簿、短信、媒体库中的数据共享给其他应用等。当然&#xff0c;不是所有的数据都允许其他应用访问&#xff0c;比…

14:00面试,15:00就出来了,问的问题过于变态了。。。

从小厂出来&#xff0c;没想到在另一家公司又寄了。 到这家公司开始上班&#xff0c;加班是每天必不可少的&#xff0c;看在钱给的比较多的份上&#xff0c;就不太计较了。没想到2月一纸通知&#xff0c;所有人不准加班&#xff0c;加班费不仅没有了&#xff0c;薪资还要降40%…