WPF DataGrid 赋值与修改

news/2024/9/21 17:21:35 标签: wpf

WPF DataGrid 赋值与修改

下方绑定给 DataGrid 控件的类模型
public record DataGridModel
  {
       public string GongNeng{get; set;}
       public DataGridModel(string gongNeng)
       {
           GongNeng = gongNeng;
       }
   }

一、DataGrid 绑定对应的属性

DataGrid 绑定列,将对应的属性绑定给对应的列
写法:
	Binding="{Binding 属性名}"  
<DataGrid.Columns>
   <!--ElementStyle 设置元素样式-->
    <DataGridTextColumn Header="功能" Width="*" Binding="{Binding GongNeng}"></DataGridTextColumn>
</DataGrid.Columns>

二、为 DataGrid 赋值

方法一
  再 cs 文件中直接给对应的 DataGrid 控件赋数据源

...
public List<DataGridModel> DataGridModels { get; set; }
...
//我这里 DataGrid 控件 Name 为 main_datagrid_display
main_datagrid_display.ItemsSource = DataGridModels;
...

方法二:
  再 xaml 文件中直接给对应的 DataGrid 控件赋数据源
  情况一:
   ItemsSource=“{Binding RelativeSource={RelativeSource AncestorType=对应数据源路径},Path=对应数据源名称}”
   如果这里数据源在父类、或者父类的父类、或者其他类中等等,需要将对应的路径指定正确

<DataGrid 
	Name="main_datagrid_display" 
	Width="1268" Height="193"  
	ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=local:MainBody},Path=DataGridModels}">
...            
</DataGrid>

情况二:
  ItemsSource=“{Binding 对应数据源名称}”
  情况一简写写法,但是必须在 cs 文件中加上
    this.DataContext = 对应数据源路径;
  正常情况下,我们会书写一个视图,将所有的数据处理都放在那个视图当中,这时数据源属性也会定义在该视图中,所有对数据的操作也会在该视图中,但是需要将 this.DataContext 赋值为该视图对象

在这里插入代码片

```csharp
//cs 文件
InitializeComponent();
//一般写在 InitializeComponent() 方法下面,我这里没有使用 MVVM 模式,所有的数据处理都在该 cs 文件中,所以指定为 this
this.DataContext =  this;
//xaml 文件
<DataGrid 
	Name="main_datagrid_display" 
	Width="1268" Height="193"  
	ItemsSource="{Binding DataGridModels}">
</DataGrid>

三、修改 DataGrid 数据

  当内部数据源修改时,如果需要前端页面也跟着一起修改,需要继承 INotifyPropertyChanged 接口,并实现 PropertyChanged 事件
  我们这里对上面的类模型进行修改:

public record DataGridModel : INotifyPropertyChanged
{
     private string _gongNeng;
     public string GongNeng
     {
         get { return _gongNeng; }
         set
         {
             if (value != _gongNeng)
             {
                 _gongNeng = value;
                 if (null != PropertyChanged)
                 {
                     PropertyChanged(this, new PropertyChangedEventArgs("GongNeng"));//当被改变时,告知前端页面
                 }
             }
         }
     }
     
     public DataGridModel(string gongNeng)
     {
          GongNeng = gongNeng;
      }
     public event PropertyChangedEventHandler? PropertyChanged;//实现 PropertyChanged 事件
 }

  如果绑定的属性是一个集合的话,也可以使用 WPF 自带的 ObservableCollection 集合,其内部帮我们实现了 INotifyCollectionChanged, INotifyPropertyChanged,当后台数据改变时,会自动帮我们更新前端的数据。
    但是使用 ObservableCollection 需要注意要为其初始化一个对象,否则会出现后端数据变了,前端数据不变化的情况,且 ObservableCollection 只有在新增或者删除一条数据的时候,才回去通知前端

ObservableCollection<string> Test { get; set; } = new ObservableCollection<string>();//需要为其初始化

  上述均是前后端不分离的情况,如果使用 MVVM 方式去书写,需要定义对应的模型视图等等,数据处理全部在视图中进行。。。


http://www.niftyadmin.cn/n/5669151.html

相关文章

C++ | Leetcode C++题解之第415题字符串相加

题目&#xff1a; 题解&#xff1a; class Solution { public:string addStrings(string num1, string num2) {int i num1.length() - 1, j num2.length() - 1, add 0;string ans "";while (i > 0 || j > 0 || add ! 0) {int x i > 0 ? num1[i] - 0 …

Linux系统中FTP操作命令

引言 在Linux系统中&#xff0c;FTP&#xff08;File Transfer Protocol&#xff09;是一种常用的文件传输协议&#xff0c;广泛应用于文件的上传和下载。无论是网站的文件管理&#xff0c;还是服务器之间的数据传输&#xff0c;掌握FTP操作命令都是Linux管理员和开发者的基本…

nginx实现轮询机制(nginx基础配置一)

一、首先在 nginx的conf配置一下代理 ###定义上游服务器(需要被nginx真实代理访问的服务器) 默认是轮训机制 upstream backServer{ server 127.0.0.1:8080; server 127.0.0.1:8081; } server { listen 80; server_name upstream.boyatop.cn; location / { ### 指定上游服务器负…

react 甘特图之旅

react-gantt GitHub 仓库: https://github.com/clayrisser/react-gantt react-gantt-chart GitHub 仓库: https://github.com/MaTeMaTuK/gantt-task-react easy-gant-beta GitHub 仓库: https://github.com/web-widgets/react-gantt-demos 上面的版本不兼容 dhtmlx-gant…

【智能大数据分析 | 实验一】MapReduce实验:单词计数

【作者主页】Francek Chen 【专栏介绍】 ⌈ ⌈ ⌈智能大数据分析 ⌋ ⌋ ⌋ 智能大数据分析是指利用先进的技术和算法对大规模数据进行深入分析和挖掘&#xff0c;以提取有价值的信息和洞察。它结合了大数据技术、人工智能&#xff08;AI&#xff09;、机器学习&#xff08;ML&a…

IS-ISv6单拓扑存在的问题

文章目录 IS-ISv6单拓扑配置单拓扑存在的问题解决 IS-ISv6单拓扑 配置 R1&#xff1a;sy sy R1 ipv6 inter g0/0/0 ip add 12.1.1.1 24 ipv6 enable ipv add 2001:12::1 64 inter loop0 ip add 1.1.1.1 32 ipv6 enable ipv address 2002::1 128isis net 49.111111111111.00 is…

每日OJ题_牛客_WY22 Fibonacci数列(斐波那契)

目录 牛客_WY22 Fibonacci数列&#xff08;斐波那契&#xff09; 解析代码 牛客_WY22 Fibonacci数列&#xff08;斐波那契&#xff09; Fibonacci数列_牛客题霸_牛客网 解析代码 求斐波那契数列的过程中&#xff0c;判断⼀下&#xff1a;何时 n 会在两个 fib 数之间。 #in…

如何将 java.nio.ByteBuffer 转为 String

如何将 java.nio.ByteBuffer 转为 String 方法1: newString()方法结合ByteBuffer的array()方法, 忽略是否flip()过 用String的 public String(byte[] bytes, int offset, int length, Charset charset)方法 和 ByteBuffer的array()方法 长度在取 bbf.position()0?bbf.limit(…