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

相关推荐
极客智造32 分钟前
深入解析 ReactiveUI:WPF 响应式 MVVM 开发的 “终极方案”
wpf
Eiceblue7 小时前
通过 C# 将 HTML 转换为 RTF 富文本格式
开发语言·c#·html
IUGEI7 小时前
synchronized的工作机制是怎样的?深入解析synchronized底层原理
java·开发语言·后端·c#
czhc11400756639 小时前
C# 1124 接收
开发语言·c#
时光追逐者11 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 62 期(2025年11.17-11.23)
c#·.net·.netcore
司铭鸿11 小时前
祖先关系的数学重构:从家谱到算法的思维跃迁
开发语言·数据结构·人工智能·算法·重构·c#·哈希算法
宝桥南山14 小时前
.NET 10 - Blazor web assembly应用的一些诊断方式
microsoft·微软·c#·asp.net·.net·.netcore
m0_6265352016 小时前
代码分析
开发语言·c#
FuckPatience18 小时前
.netcoreapp2.0与.Net Core是什么关系
c#·.net·.netcore