欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在winform中實現(xiàn)雙向數(shù)據(jù)綁定的方法

 更新時間:2024年03月19日 08:25:32   作者:Dm_dotnet  
雙向數(shù)據(jù)綁定是一種允許我們創(chuàng)建持久連接的技術(shù),使模型數(shù)據(jù)和用戶界面(UI)之間的交互能夠自動同步,今天我想通過winform中DataGridView控件為例,介紹在winform中如何實現(xiàn)雙向數(shù)據(jù)綁定,需要的朋友可以參考下

什么是雙向數(shù)據(jù)綁定?

雙向數(shù)據(jù)綁定是一種允許我們創(chuàng)建持久連接的技術(shù),使模型數(shù)據(jù)和用戶界面(UI)之間的交互能夠自動同步。這意味著當(dāng)模型數(shù)據(jù)發(fā)生變化時,UI會自動更新,反之亦然。這種雙向數(shù)據(jù)綁定極大地簡化了UI和模型數(shù)據(jù)之間的同步,使開發(fā)者可以更專注于業(yè)務(wù)邏輯,而不是手動處理UI和數(shù)據(jù)的同步。

今天我想通過winform中DataGridView控件為例,介紹在winform中如何實現(xiàn)雙向數(shù)據(jù)綁定。

一般在winform中使用DataGridView控件時,我們可能是這樣使用的:

創(chuàng)建數(shù)據(jù)源

以Person類為例:

  public class Person
  {
      public string? Name { get; set; }
      public string? Home { get; set; }
  }

創(chuàng)建Person對象列表:

  // 創(chuàng)建一個Person對象的列表
  List<Person> people = new List<Person>()
  {
      new Person {Name = "張三",Home = "武漢" },
      new Person {Name = "李四",Home = "南昌" },
      new Person {Name = "王五",Home = "福州" },
  };

綁定數(shù)據(jù)源:

dataGridView1.DataSource = people;

這個時候的效果如下所示:

當(dāng)我們進行修改之后,如下所示:

現(xiàn)在打印people列表第一個和第二個person對象的Home屬性值看看:

  Debug.WriteLine(people[0].Home);
  Debug.WriteLine(people[1].Home);

結(jié)果如下圖所示:

說明在dataGridView1上修改數(shù)據(jù),people列表也被修改了。

現(xiàn)在反過來測試一下,修改people列表第一個和第二個person對象的Home屬性值:

 people[0].Home = "廈門";
 people[1].Home = "廈門";

會發(fā)現(xiàn)dataGridView1上的數(shù)據(jù)不會發(fā)生變化,需要我們點擊對應(yīng)的空格之后才會發(fā)生改變,如下所示:

如果我們這樣寫的話:

 people[0].Home = "廈門";
 people[1].Home = "廈門";
 dataGridView1.UpdateCellValue(1,1);
 dataGridView1.UpdateCellValue(1,2);

效果如下所示:

只改變了一個空格的值,另一個還是需要點擊一下,才更新。

在winform中實現(xiàn)雙向數(shù)據(jù)綁定示例

首先創(chuàng)建一個Student類,如下所示:

 public class Student : INotifyPropertyChanged
 {
    
     private string? _name;
?
     public string Name
     {
         get { return _name; }
         set
         {
             _name = value;
             // Call OnPropertyChanged whenever the property is updated
             OnPropertyChanged("Name");
         }
     }       
     private string? _home;
?
     public string Home
     {
         get { return _home; }
         set
         {
             _home = value;
             // Call OnPropertyChanged whenever the property is updated
             OnPropertyChanged("Home");
         }
     }
?
     // Declare the event
     public event PropertyChangedEventHandler? PropertyChanged;
     // Create the OnPropertyChanged method to raise the event
     protected void OnPropertyChanged(string name)
     {
         var handler = PropertyChanged;
         handler?.Invoke(this, new PropertyChangedEventArgs(name));
     }
 }

實現(xiàn)了INotifyPropertyChanged接口。

創(chuàng)建數(shù)據(jù)源:

 // 創(chuàng)建一個Student對象的列表
 BindingList<Student> students = new BindingList<Student>()
 {
         new Student { Name = "張三", Home = "武漢" },
         new Student { Name = "李四", Home = "南昌"  },
         new Student { Name = "王五", Home = "福州"  },
 };

注意這里使用的是BindingList<T>而不是List<T>。

BindingList<T>List<T>的區(qū)別

BindingList 和 List 都是用于存儲對象的集合,但它們之間有一些關(guān)鍵的區(qū)別。

  • 數(shù)據(jù)綁定支持:BindingList 是為數(shù)據(jù)綁定設(shè)計的,它實現(xiàn)了 IBindingList 接口。這意味著當(dāng) BindingList 中的數(shù)據(jù)發(fā)生更改時(例如,添加、刪除或修改項),它會自動通知綁定到它的任何 UI 控件。這對于 Windows Forms 或 WPF 這樣的 UI 框架非常有用,因為它們可以自動更新以反映數(shù)據(jù)的更改。相比之下,List 不支持?jǐn)?shù)據(jù)綁定。
  • 事件通知:BindingList 提供了一些額外的事件,如 ListChanged,這可以讓你知道列表何時被修改。List 沒有這樣的事件。
  • 性能:由于 BindingList 提供了額外的功能,所以在某些情況下,它可能比 List 慢一些。如果你不需要數(shù)據(jù)綁定或更改通知,那么 List 可能會提供更好的性能。

綁定數(shù)據(jù)源:

   dataGridView1.DataSource = students;

更改數(shù)據(jù)源的值,查看UI是否會自動改變:

 students[0].Home = "廈門";
 students[1].Home = "廈門";  

實現(xiàn)的效果如下所示:

發(fā)現(xiàn)當(dāng)數(shù)據(jù)的值發(fā)生改變時,dataGridView1會自動進行更新。

編輯dataGridView1查看數(shù)據(jù)源是否會發(fā)生改變,編輯之后如下圖所示:

查看結(jié)果:

 Debug.WriteLine(students[0].Home);
 Debug.WriteLine(students[1].Home);

結(jié)果如下圖所示:

說明編輯dataGridView1產(chǎn)生的更改也會導(dǎo)致數(shù)據(jù)源的更改。

總結(jié)

以上就是在winform中實現(xiàn)雙向數(shù)據(jù)綁定的一次實踐,要點有兩個,第一個是類實現(xiàn)INotifyPropertyChanged,第二個是用BindingList<T>代替List<T>,希望對你有所幫助。

到此這篇關(guān)于在winform中實現(xiàn)雙向數(shù)據(jù)綁定的方法的文章就介紹到這了,更多相關(guān)winform雙向綁定數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論