1024programmer Asp.Net WPF-dataGrid dynamic update

WPF-dataGrid dynamic update

WPF-dataGrid dynamic update

Introduction:

Problem: In WPF, ObservableCollection is used as the data source of the dataGrid, and it is found that updating the data does not trigger the update of the dataGrid

By MaQaQ 2023-11-22

Analysis:

1. ObservableCollection will trigger update notifications when elements of the collection are added, removed, or cleared. Changes to the properties of elements in the collection will not trigger notifications.

2. If you want to trigger a notification when updating data, you can implement the INotifyPropertyChanged interface in the T class, so that when its properties change, the notification will be triggered and the dataGrid will be updated.

The implementation is as follows:

 1. xml

 

 2. CS medium

  • Define ObservableCollection<Model> memberData = new ObservableCollection<Model>();
  • Model is defined as follows:

public class Model : INotifyPropertyChanged
 {
     protected int  span> id;
     protected string  span> name;
     protected bool  span> online;
     protected bool  span> registered;
     public int  span> ID { get => id; set => id = value; }
     public string  span> Name { get => name; set => name = value; }
     public bool  span> IsOnline
     {
         get => online  ;
         set
         {
             if (online != value)
             {
                 online = value;
                 OnPropertyChanged("IsOnline  ");
             }
         }
     }
     public bool  span> IsRegistered
     {
         get => registered  ;
         set
         {
             if (registered != value)
             {
                 registered = value;
                 OnPropertyChanged("IsRegistered  ");
             }
         }
     }
     public event  span> PropertyChangedEventHandler PropertyChanged;
     protected void  span>OnPropertyChanged(string name)
     {
         PropertyChangedEventHandler handler = PropertyChanged;
         if (handler != null)
         {
             handler(this,   new PropertyChangedEventArgs(name));
         }
     }
 }

View Code

  • Bind dataGrid data source: dataGrid.DataContext = memberData;
  • Update memberData data:

private void ElevatorChanged(Data.ElevatorModel model)
 {
     var result1 = memberData.Count == 0 ? null : memberData.Where(p => p.Name == model.Name  ).Count() == 0 ? null : memberData.Where(p => p.Name == model.Name).First();
     if (result1 != null)
     {
         result1.ID = model.ID;
         result1.Name = model.Name;
         result1.IsOnline = model.IsOnline;
         result1.IsRegistered = model.IsRegistered;
     }
     else
     {
         memberData.Add(model);
     }
 }

View Code

In the above example, when IsOnline, IsRegistered updates or memberData.Add(model), the notification will be triggered and the display of the dataGrid will be updated.

Summary:

1. Using ObservableCollection will trigger updates when elements of the collection are added, removed or cleared
2. Using the INotifyPropertyChanged interface will trigger notifications when properties change.

This article comes from Blog Garden, author: MaQaQ, please indicate the original link when reprinting: https://www.cnblogs.com/magicMaQaQ/p/17849078.html

The copyright of this article belongs to the author and the blog park. Reprinting is welcome. However, this statement must be retained without the author’s consent, and a link to the original text must be provided in a prominent position on the article page. Otherwise, we reserve the right to pursue legal liability.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811247

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索