C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > Winform同一数据源多个控件保持同步

c# Winform同一数据源多个控件保持同步

作者:zhuanghamiao

通过对控件属性设置数据源绑定,利用Windows数据更改通知这一特性,只要订阅(设定绑定)的控件都能接收到数据的变化通知。 通过DataBindings方法实现双向数据绑定

在了解Windows窗体数据绑定直接,对控件数据绑定一般都是直接对控件可接收值的属性赋值,如文本框的Text属性,但在做一些列表数据处理时,同一份数据要多处处理,对各个控件的属性逐一赋值显得比较麻烦。通过对控件属性设置数据源绑定,利用Windows数据更改通知这一特性,只要订阅(设定绑定)的控件都能接收到数据的变化通知。
通过DataBindings方法实现双向数据绑定

textBox1.DataBindings.Add("Text", trackBar1, "Value", true, DataSourceUpdateMode.OnPropertyChanged);

实现 INotifyPropertyChanged 接口,当属性值更改时引发事件通知

private Serial serial;

public Form3()
{
    InitializeComponent();
    serial = new Serial();

    this.textBox1.DataBindings.Add("Text", serial, "Number", true, DataSourceUpdateMode.OnPropertyChanged);
}

private void button1_Click(object sender, EventArgs e)
{
    serial.Number += 1;
}
public class Serial : INotifyPropertyChanged
{
    private int number;

    public int Number
    {
        get { return number; }
        set
        {
            if (value != number)
            {
                number = value;
                NotifyPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

BindingList实现了双向Windows窗体数据绑定所需的主要接口。通过将BindingSource数据源绑定到控件,可以使多个控件保持数据同步

public partial class Form1 : Form
{
    private IList<Foo> foos;
    private BindingList<Foo> bindingList;

    public Form1()
    {
        InitializeComponent();
        dataGridView1.MultiSelect = false;
        // 数据源
        foos = new List<Foo> {
            new Foo(){Id=Guid.NewGuid().ToString(), Field1="张三",Field2="选项一",Field3=DateTime.Now.AddYears(-6),Field4="1918731555X",Field5="云南省昆明市...街道66号"},
            new Foo(){Id=Guid.NewGuid().ToString(), Field1="李四",Field2="选项二",Field3=DateTime.Now.AddYears(-7),Field4="1918731556X",Field5="云南省玉溪市...街道56号"},
            new Foo(){Id=Guid.NewGuid().ToString(), Field1="王五",Field2="选项一",Field3=DateTime.Now.AddYears(-8),Field4="1918731557X",Field5="云南省红河州...街道46号"},
            new Foo(){Id=Guid.NewGuid().ToString(), Field1="赵六",Field2="选项三",Field3=DateTime.Now.AddYears(-9),Field4="1918731558X",Field5="云南省楚雄州...街道36号"},
        };

        this.Load += Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bindingList = new BindingList<Foo>(foos);
        var source = new BindingSource(bindingList, null);
        // 控件数据绑定
        dataGridView1.DataSource = source;

        textBox1.DataBindings.Add("Text", source, "Field1", true, DataSourceUpdateMode.OnPropertyChanged);
        comboBox1.DataBindings.Add("Text", source, "Field2", true, DataSourceUpdateMode.OnPropertyChanged);
        dateTimePicker1.DataBindings.Add("Value", source, "Field3", true, DataSourceUpdateMode.OnPropertyChanged);
        textBox2.DataBindings.Add("Text", source, "Field4", true, DataSourceUpdateMode.OnPropertyChanged);
        textBox3.DataBindings.Add("Text", source, "Field5", true, DataSourceUpdateMode.OnPropertyChanged);

        bindingList.AddingNew += BindingList_AddingNew;
    }

    private void BindingList_AddingNew(object sender, AddingNewEventArgs e)
    {
        e.NewObject = new Foo() { Id = Guid.NewGuid().ToString() };
    }

    // 保存
    private void button1_Click(object sender, EventArgs e)
    {
        
    }

    // 新增
    private void button2_Click(object sender, EventArgs e)
    {
        // 方式一
        //bindingList.Add(new Foo() { Id = Guid.NewGuid().ToString() });
        //int selectIndex = dataGridView1.Rows.Count - 1;
        //dataGridView1.Rows[selectIndex].Selected = true;
        //dataGridView1.CurrentCell = dataGridView1.Rows[selectIndex].Cells[0];

        //方式二
        var foo = bindingList.AddNew();
    }

    // 删除
    private void button3_Click(object sender, EventArgs e)
    {
        bindingList.RemoveAt(dataGridView1.CurrentRow.Index);
        dataGridView1.Rows[0].Selected = true;
        dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
    }
}

class Foo
{
    public string Id { get; set; }
    public string Field1 { get; set; }
    public string Field2 { get; set; }
    public DateTime Field3 { get; set; }
    public string Field4 { get; set; }
    public string Field5 { get; set; }
}

以上就是c# Winform同一数据源多个控件保持同步的详细内容,更多关于Winform同一数据源多个控件保持同步的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文