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 }
原文地址:https://www.cnblogs.com/sunsuoli/p/13048252.html