Winforms Windows窗体绑定问题:如何知道用户是否更改了值

Winforms Windows窗体绑定问题:如何知道用户是否更改了值,winforms,events,binding,Winforms,Events,Binding,我目前有一个windows窗体应用程序,由一个文本框和两个按钮(上一个和下一个)组成。文本框绑定到列表中的一个人的名字。“上一步”和“下一步”按钮将BindingManager的位置更改1(递增或递减) 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Linq; 使用系统文本; 使用System.Windows.Forms; 命名空间Windows窗体应用程序1 { 公共部分类Form1:Form { 私

我目前有一个windows窗体应用程序,由一个文本框和两个按钮(上一个和下一个)组成。文本框绑定到列表中的一个人的名字。“上一步”和“下一步”按钮将BindingManager的位置更改1(递增或递减)

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
私人名单;
bindingManager数据库bindingManager;
公共表格1()
{
初始化组件();
stringList=新列表();
添加(newperson{name=“person1”});
添加(新人物{name=“person2”});
添加(newperson{name=“person3”});
添加(新的Person{name=“person4”});
bindingManager=this.BindingContext[stringList];
bindingManager.CurrentChanged+=handleCurrentChanged;
textBox1.DataBindings.Add(新绑定(“Text”,stringList,“name”));
}
私有void textBox1\u TextChanged(对象发送方,事件参数e)
{
}
私有void handleCurrentChanged(对象发送方,事件参数e)
{
MessageBox.Show(“handleCurrentChanged”);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
bindingManager.Position++;
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
bindingManager.Position--;
}
}
公共阶层人士
{
公共字符串名称{get;set;}
}
}
当用户按下“上一步”或“下一步”按钮时,需要提示用户是否保存更改。但前提是他对文本框做了一些修改


我的问题是如何知道Person对象是否有一些更改,以便启动提示。我本来打算使用BindingManagerBase的currentChanged事件,但这只检查您是否更改了列表中的项目。我也无法检查文本框,因为“上一个”和“下一个”按钮也会对其进行操作,我不想提示用户进行操作。

您可以在person类中实现INotifyPropertyChanged界面,然后每当person类中的属性发生更改时,都会通知GUI类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private List<Person> stringList;

        BindingManagerBase bindingManager;

        public Form1()
        {
            InitializeComponent();

            stringList = new List<Person>();
            stringList.Add(new Person{ name = "person1" });
            stringList.Add(new Person { name = "person2" });
            stringList.Add(new Person { name = "person3" });
            stringList.Add(new Person { name = "person4" });

            bindingManager = this.BindingContext[stringList];

            bindingManager.CurrentChanged += handleCurrentChanged;

            textBox1.DataBindings.Add(new Binding("Text", stringList, "name"));


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void handleCurrentChanged(object sender, EventArgs e)
        {
            MessageBox.Show("handleCurrentChanged");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bindingManager.Position++;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bindingManager.Position--;
        }
    }

    public class Person
    {
        public String name { get; set; }
    }
}