• 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.运行结果

  • 相关阅读:
    threeJS嵌入可交互的普通页面
    arm 汇编基础指令
    记录C文件到可执行二进制文件的经历过程
    决策树(DT)相关介绍,实现肌电信号(8类)分类
    centos 6.10 安装 perl 5.14
    Reactor网络模式
    computer planetary MoBI:生物多样性重要性地图
    在克隆项目时出现Failed to connect to github.com port 443:connection timed out
    UE5.1 透明渲染流程框架图
    Kafka详解
  • 原文地址:https://blog.csdn.net/xie__jin__cheng/article/details/137407445