C#-WPF之后台實現數據綁定,不修改xaml文件


小白入門,看到各種大神一頓操作,越看越懵逼,自己摸索半天終於找到了一個不修改xaml文件的數據綁定套路,深入的原理沒搞明白,反正這樣一頓套路操作就能實現數據綁定

下面以將電腦的串口名綁定到ComboBox為例,記錄一下數據綁定:

1.先創建一個class用來包含需要綁定的數據

  四個重要點:1.類要繼承自“: INotifyPropertyChanged”

        2.類里添加一句話 public event PropertyChangedEventHandler PropertyChanged;

        3.創建數據的get、set

        4.set里增加屬性更改函數

代碼如下:

 1 public class Ports : INotifyPropertyChanged
 2     {
 3         public event PropertyChangedEventHandler PropertyChanged;
 4 
 5         private string[] _comports;//定義串口端口
 6         public string[] comports
 7         {
 8             get
 9             {
10                 return _comports;
11             }
12             set
13             {
14                 _comports = value;
15                 if (PropertyChanged != null)
16                 {
17                     PropertyChanged(this, new PropertyChangedEventArgs("comports"));
18                 }
19             }
20         }
21 
22     }

2.將數據綁定到要顯示的控件上

  幾個重要點:1.把步驟1創建的類實例化

        2.實例化綁定類

        3.設置綁定數據源

        4.設置綁定數據路徑

        5.將數據綁定到控件

代碼如下:

 1 public partial class MainWindow : Window
 2     {
 3         Ports p = new Ports();
 4         public MainWindow()
 5         {
 6             InitializeComponent();
 7 
 8             Binding binding = new Binding();
 9             binding.Source = p;
10             binding.Path = new PropertyPath("comports");
11             this.ComPort.SetBinding(ComboBox.ItemsSourceProperty, binding);
12         }
13     }

3.通過操作數據更改控件顯示(refresh是一個鼠標按下事件)

1 private void refresh(object sender, EventArgs e)
2         {
3             string[] ArryPort = SerialPort.GetPortNames();
4             p.comports = ArryPort;
5         }

4.全部代碼如下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.IO.Ports;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using System.Windows;
 9 using System.Windows.Controls;
10 using System.Windows.Data;
11 using System.Windows.Documents;
12 using System.Windows.Input;
13 using System.Windows.Media;
14 using System.Windows.Media.Imaging;
15 using System.Windows.Navigation;
16 using System.Windows.Shapes;
17 
18 namespace 調試工具
19 {
20     /// <summary>
21     /// MainWindow.xaml 的交互邏輯
22     /// </summary>
23     public partial class MainWindow : Window
24     {
25         Ports p = new Ports();
26         public MainWindow()
27         {
28             InitializeComponent();
29 
30             Binding binding = new Binding();
31             binding.Source = p;
32             binding.Path = new PropertyPath("comports");
33             this.ComPort.SetBinding(ComboBox.ItemsSourceProperty, binding);
34         }
35 
36 
37         private void refresh(object sender, EventArgs e)
38         {
39             string[] ArryPort = SerialPort.GetPortNames();
40             p.comports = ArryPort;
41         }
42     }
43     public class Ports : INotifyPropertyChanged
44     {
45         public event PropertyChangedEventHandler PropertyChanged;
46 
47         private string[] _comports;//定義串口端口
48         public string[] comports
49         {
50             get
51             {
52                 return _comports;
53             }
54             set
55             {
56                 _comports = value;
57                 if (PropertyChanged != null)
58                 {
59                     PropertyChanged(this, new PropertyChangedEventArgs("comports"));
60                 }
61             }
62         }
63 
64     }
65 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM