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>
相关推荐
葬歌倾城7 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
Eiceblue9 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王9 小时前
hello判断
开发语言·c#
共享ui设计和前端开发人才9 小时前
UI前端大数据处理安全性保障:数据加密与隐私保护策略
ui
ui设计前端开发老司机9 小时前
UI前端大数据处理性能瓶颈突破:分布式计算框架的应用
ui
前端开发与ui设计的老司机9 小时前
UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
前端·ui
金增辉11 小时前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#
future141213 小时前
C#学习日记
开发语言·学习·c#
UI设计和前端开发从业者13 小时前
从UI设计到数字孪生实战应用:构建智慧金融的智能风控平台
ui·金融
傻啦嘿哟14 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#