当前位置: 首页 > news >正文

C# WPF 数据绑定

需求

后台变量发生改变,前端对应的相关属性值也发生改变

实现

接口 INotifyPropertyChanged 用于通知客户端(通常绑定客户端)属性值已更改。

示例

示例一

官方示例代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//将下面的命名空间更改为项目的名称
//或者在创建项目时使用以下名称命名项目。
namespace TestNotifyPropertyChangedCS
{//这个表单演示了如何使用BindingSource来绑定//一个列表到DataGridView控件。这个列表没有//引发更改通知。然而,DemoCustomer类型//在列表中。  public partial class Form1 : Form{//此按钮可更改列表元素的值。 private Button changeItemBtn = new Button();// 这个DataGridView控件显示列表的内容。private DataGridView customersDataGridView = new DataGridView();// 这个BindingSource将列表绑定到DataGridView控件。private BindingSource customersBindingSource = new BindingSource();public Form1(){InitializeComponent();// 设置“更改项目”按钮。this.changeItemBtn.Text = "Change Item";this.changeItemBtn.Dock = DockStyle.Bottom;this.changeItemBtn.Click +=new EventHandler(changeItemBtn_Click);this.Controls.Add(this.changeItemBtn);//设置DataGridView。  customersDataGridView.Dock = DockStyle.Top;this.Controls.Add(customersDataGridView);this.Size = new Size(400, 200);}private void Form1_Load(object sender, EventArgs e){//创建并填充DemoCustomer对象列表//它将向DataGridView提供数据。 BindingList<DemoCustomer> customerList = new BindingList<DemoCustomer>();customerList.Add(DemoCustomer.CreateNewCustomer());customerList.Add(DemoCustomer.CreateNewCustomer());customerList.Add(DemoCustomer.CreateNewCustomer());// 将列表绑定到BindingSource。  this.customersBindingSource.DataSource = customerList;// 将BindingSource附加到DataGridView。this.customersDataGridView.DataSource =this.customersBindingSource;}//修改CompanyName属性的值//当点击“更改项目”按钮时,列表中的项目。  void changeItemBtn_Click(object sender, EventArgs e){//从BindingSource获取对列表的引用。 BindingList<DemoCustomer> customerList =this.customersBindingSource.DataSource as BindingList<DemoCustomer>;//修改CompanyName属性的值//列表中的第一项。  customerList[0].CustomerName = "Tailspin Toys";customerList[0].PhoneNumber = "(708)555-0150";}}//这是一个简单的客户类//实现IPropertyChange接口。public class DemoCustomer : INotifyPropertyChanged{//这些字段保存公共属性的值。  //Guid.NewGuid();随机生成唯一标识符,一般在项目中用来生成数据库的主键Id。private Guid idValue = Guid.NewGuid();private string customerNameValue = String.Empty;private string phoneNumberValue = String.Empty;public event PropertyChangedEventHandler PropertyChanged;//该方法由每个属性的Set访问器调用。//应用于可选propertyName的CallerMemberName属性//参数导致调用方的属性名被替换为参数。private void NotifyPropertyChanged([CallerMemberName] String propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}//构造函数是私有的,用来强制工厂模式。  private DemoCustomer(){customerNameValue = "Customer";phoneNumberValue = "(312)555-0100";}//这是一个公共工厂方法。public static DemoCustomer CreateNewCustomer(){return new DemoCustomer();}//该属性表示一个ID,合适//用作数据库中的主键。  public Guid ID{get{return this.idValue;}}public string CustomerName{get{return this.customerNameValue;}set{if (value != this.customerNameValue){this.customerNameValue = value;NotifyPropertyChanged();}}}public string PhoneNumber{get{return this.phoneNumberValue;}set{if (value != this.phoneNumberValue){this.phoneNumberValue = value;NotifyPropertyChanged();}}}}
}

示例演示

before

after

示例二

本示例提供了多种绑定方式,使用接口进行绑定,不使用接口进行绑定

1.在MainWindow中进行属性更改

2.在MainWindow中建立一个新的usercontrol查看是否可以进行相同的属性更改

3.原始示例中展示了Text的文字内容更改,尝试对DataTrigger的Binding使用同样的方法查看是否可以更改

后端代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;namespace WpfApp1
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window, INotifyPropertyChanged{public MainWindow(){this.DataContext = thi
http://www.lryc.cn/news/279581.html

相关文章:

  • 进程和线程的比较
  • 深入理解 Flink(四)Flink Time+WaterMark+Window 深入分析
  • 科技创新领航 ,安川运动控制器为工业自动化赋能助力
  • 图像异或加密及唯密文攻击
  • React Grid Layout基础使用
  • 第11章 1 文件及IO操作
  • Tomcat服务实例部署
  • 高精度彩色3D相机:开启崭新的彩色3D成像时代
  • 借助Gitee将typora图片上传CSDN
  • 几件奇怪的事产生的疑团
  • 陶瓷碗口缺口检测-图像增强
  • gitee创建远程仓库并克隆远程仓库到电脑
  • 3D人体姿态估计(教程+代码)
  • Python异步编程|PySimpleGUI界面读取PDF转换Excel
  • 制造领域 基础概念快速入门介绍
  • 小程序的完整开发流程?
  • 【LV13 DAY16 轮询与中断】
  • Swoft - Bean
  • 【产品人卫朋】硬件产品经理:从入门到精通
  • swing快速入门(四十)JList、JComboBox实现列表框
  • React Native 原生组件回调JS层方法和 JS 层调用原生组件的事件方法
  • Go-安装与基础语法
  • 【同济子豪兄斯坦福CS224W中文精讲】NetworkX代码学习笔记
  • java+ssm+vue代码视频学习讲解
  • [计算机提升] 创建FTP共享
  • R语言将list转变为dataframe(常用)
  • 【JAVA】OPENGL+TIFF格式图片,不同阈值旋转效果
  • Linux系统中使用ln命令创建软连接
  • Spark---RDD(Key-Value类型转换算子)
  • 后台代码New出来DataGridTextColumn 动态添加到DataGrain 设置 Margin属性