从.Net对象到界面元素的绑定
这是 WPF 最核心、最常用、MVVM 架构基础的数据绑定方式:
把后台 C# 类、对象、属性 → 绑定到前台界面控件
我会用最通俗、从零开始、可直接复制运行的方式讲透。
一、什么是 .NET 对象 → 界面元素绑定?
一句话定义
让界面显示后台对象的数据,并且数据变化时界面自动刷新。
- 后台:一个普通 C# 类(学生、用户、商品)
- 前台:TextBlock、TextBox、ListBox 等控件
- 绑定:自动连接两者,无需手动赋值
这就是 WPF 强大的数据驱动 UI。
二、绑定必须的 4 个步骤(固定流程)
要完成 .NET 对象绑定界面,只需要 4 步:
- 创建一个 .NET 类(数据模型)
- 给类实现自动更新接口(INotifyPropertyChanged)
- 创建类的对象,并赋值给 DataContext
- 界面用 {Binding 属性名} 绑定
三、核心知识点(必须懂)
1. 关键接口:INotifyPropertyChanged
作用:后台数据变了 → 通知界面自动刷新
不实现这个接口,界面不会自动更新!
不实现这个接口,界面不会自动更新!
2. 关键上下文:DataContext
作用:告诉界面
“我要绑定的数据源,就是这个对象”
“我要绑定的数据源,就是这个对象”
3.绑定语法
1 {Binding 属性名}
四、完整实战示例(可直接运行)
步骤 1:创建 C# 数据类 + 自动更新
这是最标准的可绑定模型,所有项目都这么写:
1 using System.ComponentModel; 2 using System.Runtime.CompilerServices; 3 4 // 1. 实现 INotifyPropertyChanged 接口 5 public class Student : INotifyPropertyChanged 6 { 7 private string _name; 8 private int _age; 9 10 // 属性 11 public string Name 12 { 13 get => _name; 14 set 15 { 16 _name = value; 17 OnPropertyChanged(); // 通知界面更新 18 } 19 } 20 21 public int Age 22 { 23 get => _age; 24 set 25 { 26 _age = value; 27 OnPropertyChanged(); 28 } 29 } 30 31 // 固定写法:通知事件 32 public event PropertyChangedEventHandler PropertyChanged; 33 34 // 自动获取属性名(不用手写字符串) 35 protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 36 { 37 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 38 } 39 }
步骤 2:窗口后台创建对象并绑定
1 public MainWindow() 2 { 3 InitializeComponent(); 4 5 // 2. 创建对象 6 Student student = new Student 7 { 8 Name = "张三", 9 Age = 18 10 }; 11 12 // 3. 关键:设置 DataContext(数据源) 13 this.DataContext = student; 14 }
步骤 3:界面 XAML 绑定
1 <Grid> 2 <StackPanel> 3 <!-- 4. 绑定:{Binding 属性名} --> 4 <TextBlock Text="姓名:" FontSize="20"/> 5 <TextBox Text="{Binding Name}" FontSize="20"/> 6 7 <TextBlock Text="年龄:" Margin="0,10,0,0" FontSize="20"/> 8 <TextBox Text="{Binding Age}" FontSize="20"/> 9 10 <!-- 实时显示绑定结果 --> 11 <TextBlock Text="{Binding Name}" 12 Foreground="Blue" 13 FontSize="25" 14 Margin="0,10,0,0"/> 15 </StackPanel> 16 </Grid>
五、运行效果(超级强大)
- 界面一打开,自动显示:张三、18
- 修改输入框 → 后台对象数据自动改变
- 后台修改数据 → 界面自动刷新
- 纯自动同步,无需任何赋值代码
六、进阶示例:双向绑定(TwoWay)
TextBox 必须用双向绑定,否则输入不会同步回后台:
1 <TextBox Text="{Binding Name, Mode=TwoWay}" />
WPF 默认对 TextBox 就是 TwoWay,所以可以省略。
七、高级示例:绑定集合到列表(ListBox)
1. 后台创建集合
1 public MainWindow() 2 { 3 InitializeComponent(); 4 5 var students = new List<Student> 6 { 7 new Student{Name="张三", Age=18}, 8 new Student{Name="李四", Age=19}, 9 new Student{Name="王五", Age=20} 10 }; 11 12 this.DataContext = students; 13 }
2. 界面绑定集合
1 <ListBox ItemsSource="{Binding}" DisplayMemberPath="Name"/>
八、让集合也自动刷新:ObservableCollection
如果希望添加 / 删除项目时界面自动刷新,必须用:
1 // 而不是 List<T> 2 ObservableCollection<Student> Students { get; set; }
九、绑定常用标记(必背)
| 语法 | 作用 |
|---|---|
{Binding Name} |
绑定 Name 属性 |
{Binding Name, Mode=TwoWay} |
双向同步 |
{Binding Name, UpdateSourceTrigger=PropertyChanged} |
输入时实时更新 |
{Binding Name, StringFormat={0}岁} |
格式化显示 |
十、核心总结(1 分钟记住)
.NET 对象绑定界面 = 4 句话
- 类要继承 INotifyPropertyChanged
- 属性赋值时调用 OnPropertyChanged
- DataContext = 你的对象
- 界面写 {Binding 属性名}
最终效果
✅ 后台数据变 → 界面自动变
✅ 界面输入变 → 后台自动变
✅ MVVM 架构的核心基础
✅ 无后台赋值代码,纯自动同步
浙公网安备 33010602011771号