c# wpf datagrid 简单试验

1.概要

datagrid 一个列表类的控件

2.代码

复制代码
<Window x:Class="WpfApp2.Window3"
        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="Window3" Name="win" Height="450" Width="800">
    <Grid>
        <DataGrid Name="dataview" >
            
        </DataGrid>
    </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.Imaging;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Window3.xaml 的交互逻辑
    /// </summary>
    public partial class Window3 : Window
    {
        public List<Book> BookList = new List<Book>();
        public Window3()
        {
            InitializeComponent();
            initView();
            dataview.ItemsSource = BookList;
        }

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

相关推荐
烛阴2 小时前
Unity资源加载进化论:从AssetBundle到Addressables,一文带你吃透手游资源管理
前端·c#·unity3d
c#上位机4 小时前
wpf之RadialGradientBrush径向渐变画刷
wpf
aini_lovee5 小时前
C#与倍福PLC(通过ADS协议)通信上位机源程序实现
开发语言·c#
2501_930707786 小时前
使用C#代码压平 PDF 表单字段
数据库·pdf·c#
IT知识分享9 小时前
数字上标、下标如何打,6种常用方法详解
开发语言·c#·xhtml
码农学院10 小时前
itextsharp .net中如何设置两个表格的间距设为0,取网站的域名,协议、端口、当前站点目录的地址
开发语言·c#·.net
monkeyhlj10 小时前
Agent Skills简单理解
开发语言·c#
星河耀银海11 小时前
Unity C#入门:变量的定义与访问权限(public/private)
unity·c#·lucene
asdzx6711 小时前
使用 C# 添加或读取 Excel 公式:完整指南
开发语言·c#·excel
加号311 小时前
【C#】 中 BCD 字节数组转十进制字符串的原理与实现思路
开发语言·c#