控件和对象双向数据绑定
实现结果:
1. 对象值 -> 控件值
2. 控件值 -> 对象值

演示代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 控件与对象的双向绑定
{
public partial class Form1 : Form
{
UserInfo user = new UserInfo();
public Form1()
{
InitializeComponent();
user.UserName = "勇哥";
user.Id = 1;
//user.PropertyChanged += User_PropertyChanged;
}
public void MyMethod(string paramName = nameof(MyMethod))
{
Console.WriteLine($"The name of the parameter is: {paramName}");
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
//LabUserName.Text = user.UserName;
//LabID.Text = user.Id.ToString();
}
private void label4_Click(object sender, EventArgs e)
{
}
int js1 = 0;
private void button2_Click(object sender, EventArgs e)
{
user.UserName = $"刘备{++js1}";
user.Id = 1+js1;
}
MyClass mycls = new MyClass();
private void Form1_Load(object sender, EventArgs e)
{
BindingSource source = new BindingSource();
source.DataSource = mycls;
this.trackBar1.DataBindings.Add("Value", source, "Value1", true, DataSourceUpdateMode.OnPropertyChanged);
this.textBox1.DataBindings.Add("Text", source, "Value1", true, DataSourceUpdateMode.OnPropertyChanged);
this.LabValue.DataBindings.Add("Text", source, "Value1", true, DataSourceUpdateMode.OnPropertyChanged);
BindingSource source2 = new BindingSource();
source2.DataSource = user;
TxtUserName.DataBindings.Add("Text", source2, "UserName", true, DataSourceUpdateMode.OnPropertyChanged);
TxtId.DataBindings.Add("Text", source2, "Id", true, DataSourceUpdateMode.OnPropertyChanged);
LabUserName.DataBindings.Add("Text", source2, "UserName", true, DataSourceUpdateMode.OnPropertyChanged);
LabID.DataBindings.Add("Text", source2, "Id", true, DataSourceUpdateMode.OnPropertyChanged);
}
public class MyClass
{
public int Value1 { get; set; }
}
public class UserInfo : INotifyPropertyChanged
{
private string userName;
private int id;
public event PropertyChangedEventHandler PropertyChanged;
public string UserName
{
get
{
return userName;
}
set
{
userName = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(userName));
}
}
public int Id
{
get
{
return id;
}
set
{
id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(id.ToString()));
}
}
}
}
}几点说明:
(1)对控件的修改可以实时反馈到类对象上。但当我们修改类对象属性时,控件在这时不会更改,如果想要类属性修改是可以实时反馈到控件上,我们需要实现 INotifyPropertyChanged接口。
(2) DataBindings.Add方法有6个重载。分别是:属性名、数据源、数据成员、格式化使能、数据源更新模式、数据源为空时的默认值。因此,我们可以这样写:
private void Form1_Load(object sender, EventArgs e)
{
BindingSource source = new BindingSource();
source.DataSource = this.trackBar1;
this.textBox1.DataBindings.Add("Text", source, "Value");
this.label1.DataBindings.Add("Text", source, "Value");
}(3)数据源更新模式有三种
1。OnValidation 验证时更新数据源

验证时更新数据源,在textbox控件失去焦点时,触发Onvalidation事件,继而更改TrackBar控件value属性。默认模式

2。OnPropertyChanged 立即更新数据源

在我修改textbox控件值时,会立即修改TrackBar控件value值

3。Never 单向绑定

修改Textbox值时,TrackBar控件value属性不会更改。

---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!


少有人走的路


















