Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据绑定有问题,请解释_C#_.net_Winforms_Data Binding - Fatal编程技术网

C# 数据绑定有问题,请解释

C# 数据绑定有问题,请解释,c#,.net,winforms,data-binding,C#,.net,Winforms,Data Binding,我想我想要达到的目标是非常清楚的。我希望我的表单将两个文本框中所做的更改保存到myClass。但是,每当我在编辑两个文本框后按下save按钮,并调用saveButton\u Click,第二个textBox2的text就会返回到原始文本(“两个”)。我尝试使用Binding的WriteValue函数,但同样的情况也发生了。使用.NET4.0 编辑谢谢你的回答,但我不需要解决办法。我自己能找到。我只需要稍微了解一下绑定是如何工作的。我想了解为什么会发生这种情况?您需要更改数据绑定的方式。试试这个

我想我想要达到的目标是非常清楚的。我希望我的表单将两个
文本框中所做的更改保存到
myClass
。但是,每当我在编辑两个文本框后按下save按钮,并调用
saveButton\u Click
,第二个
textBox2
text
就会返回到原始文本(“两个”)。我尝试使用
Binding
WriteValue
函数,但同样的情况也发生了。使用.NET4.0


编辑谢谢你的回答,但我不需要解决办法。我自己能找到。我只需要稍微了解一下绑定是如何工作的。我想了解为什么会发生这种情况?

您需要更改数据绑定的方式。试试这个

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);
    }
}
关键是DataSourceUpdateMode,这将允许textbox的text属性在属性更改时级联到自定义对象中。我希望这有帮助

[编辑]

我相信你需要做的是实现你的要求:
TextBox1.DataBindings[0].ControlUpdateMode=ControlUpdateMode.Never

这应该提供一些额外的信息

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

处理此问题的一种方法是创建一个,设置
BindingSource.DataSource=myClass
,然后将文本框绑定到
BindingSource

BindingSource
如果基础数据源是一个列表,并且添加、删除了项等,或者
DataSource
属性发生更改,则会引发事件。通过设置为
false
,可以抑制这些事件,这将允许您在
myClass
上设置多个属性,而无需数据绑定更新绑定控件

MyClass myClass = new MyClass("one", "two");

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

private void saveButton_Click(object sender, EventArgs e)
{
    // your object should already have new text values entered.
    // Save your object!

    //myClass.Text1 = textBox1.Text;
    //myClass.Text2 = textBox2.Text;
    //textBox1.DataBindings["Text"].WriteValue();
    //textBox2.DataBindings["Text"].WriteValue();
}

HTH

但我不希望当
文本框的
文本更改时保存数据,我希望在按下保存按钮时更改数据。我该怎么做?难道不是
DataSourceUpdateMode。决不与
WriteValue()
一起使用来实现这种功能吗?不要使用数据绑定。只需手动将表单构造函数中的文本值分配给文本框,如下所示:
textBox1.text=myClass.Text1
@jsoldi-然后不要对这些值使用数据绑定-在saveButton\u Click事件中设置值。这就是数据绑定的目的——它将控件实例的属性绑定到数据对象的属性。但是我希望,如果我必须让windows编辑相同的
MyClass
对象,并且其中一个保存更改,那么另一个会自动更新。我可以找到一种解决方法,比如在临时字符串中保存
textBox2.Text
,然后在
saveButton\u单击
中分配它,但我的问题是为什么会发生这种情况?应该是这样吗?是的,这就是数据绑定开箱即用的方式。看看我上一次的编辑,这应该能达到你想要的效果。但是我不明白为什么我的属性不是列表的一部分。@jsoldi,我编辑了我的答案,以包括这样一个事实:
ListChanged
DataSource
属性更改时也会出现。
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;
    }
}