数据绑定问题,请解释

2024-05-27

public partial class Form1 : Form
{
    MyClass myClass = new MyClass("one", "two");

    public Form1()
    {
        InitializeComponent();
        textBox1.DataBindings.Add("Text", myClass, "Text1", false, DataSourceUpdateMode.Never);
        textBox2.DataBindings.Add("Text", myClass, "Text2", false, DataSourceUpdateMode.Never);
    }

    private void saveButton_Click(object sender, EventArgs e)
    {
        myClass.Text1 = textBox1.Text;
        myClass.Text2 = textBox2.Text;
        //textBox1.DataBindings["Text"].WriteValue();
        //textBox2.DataBindings["Text"].WriteValue();
    }
}

public class MyClass : INotifyPropertyChanged
{
    private string _Text1;
    private string _Text2;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Text1
    {
        get { return _Text1; }
        set { _Text1 = value; OnPropertyChanged(new PropertyChangedEventArgs("Text1")); }
    }

    public string Text2
    {
        get { return _Text2; }
        set { _Text2 = value; OnPropertyChanged(new PropertyChangedEventArgs("Text2")); }
    }

    public MyClass(string text1, string text2)
    {
        Text1 = text1;
        Text2 = text2;
    }

    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null) PropertyChanged(this, e);
    }
}

我认为我想要实现的目标非常清楚。我希望我的表单能够保存我的两个表单中所做的更改TextBoxes to myClass。但是每当我在编辑两个文本框后按下保存按钮时,saveButton_Click被调用,第二个textBox2's Text回到原文(“二”)。我尝试使用Binding's WriteValue函数,但同样的事情发生了。使用.net 4.0。

Edit感谢您的回答,但我不需要解决方法。我可以自己找到他们。我只需要更好地了解绑定的工作原理。我想了解为什么会发生这种情况?


显然,更新数据源上的任何值都会导致所有绑定更新。这解释了行为(设置myClass.Text1 causes textBox2更新为当前值myClass.Text2)。不幸的是,我能找到的几篇文章几乎只是说“这就是它的工作原理”。

处理这个问题的一种方法是创建一个绑定源 http://msdn.microsoft.com/en-us/library/ms158145.aspx, set BindingSource.DataSource = myClass,然后将您的文本框绑定到BindingSource.

BindingSource raises 列表已更改 http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.listchanged.aspx如果基础数据源是列表并且添加、删除项目等,则发生事件,or if the DataSource属性改变。您可以通过设置来抑制这些事件BindingSource.RaiseListChangedEvents http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.raiselistchangedevents.aspx to false,这将允许您设置多个属性myClass无需数据绑定即可更新绑定控件。

public partial class Form1 : Form
{
    MyClass myClass = new MyClass("one", "two");
    BindingSource bindingSource = new BindingSource();

    public Form1()
    {
        InitializeComponent();

        bindingSource.DataSource = myClass;

        textBox1.DataBindings.Add("Text", bindingSource, "Text1", true, DataSourceUpdateMode.Never);
        textBox2.DataBindings.Add("Text", bindingSource, "Text2", true, DataSourceUpdateMode.Never);                
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bindingSource.RaiseListChangedEvents = false;
        myClass.Text1 = textBox1.Text;
        myClass.Text2 = textBox2.Text;
        bindingSource.RaiseListChangedEvents = true;
    }
}

HTH

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据绑定问题,请解释 的相关文章

随机推荐