54.DataGrid数据框图 C#例子 WPF例子

首先是绑定一个属性,属性名称无所谓。到时候看属性设置的啥,可能要改。

XML 复制代码
<DataGrid ItemsSource="{Binding Index_instance}"/>

然后创建INotifyPropertyChanged的类,并把相关固定的代码粘贴上去。

然后把这个目录类建好,要用

cs 复制代码
    public class Index1
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }


    }

用这个目录类创建属性

cs 复制代码
 private ObservableCollection<Index1> _index_instance;
        public ObservableCollection<Index1> Index_instance
        {
            get { return _index_instance; }
            set
            {
                _index_instance = value;
                OnPropertyChanged(nameof(Index_instance));
            }
        }

再创建构造函数,搞三个实例,并赋值。

cs 复制代码
        public Notify()
        {
            Index_instance = new ObservableCollection<Index1>
            {
                new Index1() {Id= 1, Name="Test 1", Status="Active" },
                new Index1() {Id= 2, Name="Test 2", Status="Inactive" },
                new Index1() {Id= 3, Name="Test 3", Status="Bad" },
            };

        }

最后一步,把窗口资源导向这个类的实例

cs 复制代码
DataContext = new Notify();

后台代码:

cs 复制代码
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
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.Navigation;
using System.Windows.Shapes;

namespace DataGrid练习
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new Notify();
        }


    }

    public class Notify : INotifyPropertyChanged
    {
        private ObservableCollection<Index1> _index_instance;
        public ObservableCollection<Index1> Index_instance
        {
            get { return _index_instance; }
            set
            {
                _index_instance = value;
                OnPropertyChanged(nameof(Index_instance));
            }
        }


        public Notify()
        {
            Index_instance = new ObservableCollection<Index1>
            {
                new Index1() {Id= 1, Name="Test 1", Status="Active" },
                new Index1() {Id= 2, Name="Test 2", Status="Inactive" },
                new Index1() {Id= 3, Name="Test 3", Status="Bad" },
            };

        }







        //固定部分
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Index1
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }


    }
}

XAML部分:

XML 复制代码
<Window x:Class="DataGrid练习.MainWindow"
        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:DataGrid练习"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid Margin="30">
        <DataGrid ItemsSource="{Binding Index_instance}"/>
    </Grid>
</Window>
相关推荐
yuanpan25 分钟前
.net/C#进程间通信技术方案总结
开发语言·c#·.net
IM1GENIUS3 小时前
.NET高频技术点(持续更新中)
c#·.net
red-fly3 小时前
c#修改ComboBox当前选中项的文本
c#·combobox
wangnaisheng4 小时前
【WPF】Opacity 属性的使用
wpf
bicijinlian7 小时前
.Net HttpClient 概述
c#·.net·httpclient·.net httpclient
码观天工7 小时前
.NET 原生驾驭 AI 新基建实战系列(七):Weaviate ── 语义搜索的智能引擎创新者
ai·c#·.net·向量数据库·weaviate
Zhen (Evan) Wang8 小时前
.NET 8 + Angular WebSocket 高并发性能优化
c#·.net·angular
chenyuhao20248 小时前
链表面试题7之相交链表
数据结构·算法·链表·面试·c#
姬激薄9 小时前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)9 小时前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos