当前位置: 首页 > article >正文

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

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

<DataGrid ItemsSource="{Binding Index_instance}"/>

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

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

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


    }

用这个目录类创建属性

 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" },
            };

        }

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

DataContext = new Notify();

后台代码:

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部分:

<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>


http://www.kler.cn/a/514327.html

相关文章:

  • HTML中相对路径和绝对路径详解
  • react中hooks之useId用法总结以及与useRef用法区别
  • LeetCode hot 力扣100 LRU 缓存
  • 升级到MySQL 8.4,MySQL启动报错:io_setup() failed with EAGAIN
  • KubeKey安装K8s和kubesphere
  • ConvBERT:通过基于跨度的动态卷积改进BERT
  • 【Linux入门】2w字详解yum、vim、gcc/g++、gdb、makefile以及进度条小程序
  • 电脑办公技巧之如何在 Word 文档中添加文字或图片水印
  • 使用Sum计算Loss和解决梯度累积(Gradient Accumulation)的Bug
  • C# LINQ(Language Integrated Query)详解
  • docker部署的gitlab迁移
  • 图像点处理
  • 使用 vllm 部署 MiniCPM-o 2.6
  • Logo语言的操作系统
  • PostIn安装教程
  • Windows电脑安装USB Redirector并实现内外网跨网USB共享通信访问
  • Python爬虫学习第一弹 —— 爬虫的基础知识 及 requests基础
  • 深入理解机器学习中的零样本、少样本与微调
  • 金融场景 PB 级大规模日志平台:中信银行信用卡中心从 Elasticsearch 到 Apache Doris 的先进实践
  • uniapp的插件开发发布指南