• c# wpf template ItemTemplate 简单试验


    1.概要

     ItemTemplate,定义列表类的控件形状

    2.代码

    2.1 控件

    1. <Window x:Class="WpfApp2.Window2"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:WpfApp2"
    7. mc:Ignorable="d"
    8. Title="Window2" Name="win" Height="450" Width="800">
    9. <Window.Resources>
    10. <DataTemplate x:Key="MyDataTemplate">
    11. <StackPanel Orientation="Horizontal">
    12. <Border Background="Pink">
    13. <TextBlock Text="{Binding Title}"/>
    14. </Border>
    15. <Button Content="{Binding Author}" Cursor="Hand" Margin="10,0"/>
    16. </StackPanel>
    17. </DataTemplate>
    18. </Window.Resources>
    19. <Grid>
    20. <ListBox Name="nsmeList" Grid.Row="1" ItemsSource="{Binding BookList,ElementName=win}" ItemTemplate="{StaticResource MyDataTemplate}"/>
    21. </Grid>
    22. </Window>

    2.2 代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    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.Imaging;
    using System.Windows.Shapes;

    namespace WpfApp2
    {
        ///


        /// Window2.xaml 的交互逻辑
        ///

        public partial class Window2 : Window
        {
            public List BookList { get; set; } = new List();
            public Window2()
            {
                InitializeComponent();
                initView();
            }
            
            public void initView()
            {
                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) });
            }
        }
        public class Book
        {
            public required string Title { get; set; }
            public required string Author { get; set; }
            public DateTime Time { get; set; }
        }
    }
     

     

    3.运行结果

  • 相关阅读:
    Linux调试器--gdb使用
    ES全文检索支持繁简和IK分词检索
    【DL论文精读笔记】Image Segmentation Using Deep Learning: A Survey 图像分割综述
    最廉价的5.1家庭影院系统解决方案
    网站推广爬虫
    “拨”取数字的典例:N位水仙花数判断及水仙花数变种
    Scratch软件编程等级考试二级——20210911
    Stable Diffusion WebUI提示词Prompts常用推荐
    RESR开发
    C#多线程之(Thread)详解与示例
  • 原文地址:https://blog.csdn.net/xie__jin__cheng/article/details/137407445