数据绑定到一个对象在C#

数据绑定到一个对象在C#

问题描述:

目标-C /可可提供的结合,其中一个控件的属性(即文本框的文本)可以被绑定到一个对象的属性的形式。我试图复制在C#中此功能瓦特/ NET 3.5的。

Objective-c/cocoa offers a form of binding where a control's properties (ie text in a textbox) can be bound to the property of an object. I am trying to duplicate this functionality in C# w/ .Net 3.5.

我创建了以下非常简单的类在文件MyClass.cs:

I have created the following very simple class in the file MyClass.cs:

class MyClass
{
    private string myName;

    public string MyName
    {
        get
        {
            return myName;
        }

        set
        {
            myName = value;
        }
    }

    public MyClass()
    {
        myName = "Allen";
    }
}



我还创建了一个简单的形式与1文本框和1按钮。我init'd MYCLASS的一个实例的形式代码中并建项目。使用VS​​2008中的数据源向导,我选择了基于对象来创建数据源,并选择了MyClass的组装。这创造了一个数据源的实体。我改变了文本框此数据源的数据绑定;但是,预期的结果(即文本框的内容将是艾伦)没有实现。此外,将文本到文本框中没有更新对象的name属性。

I also created a simple form with 1 textbox and 1 button. I init'd one instance of Myclass inside the form code and built the project. Using the DataSource Wizard in Vs2008, I selected to create a data source based on object, and selected the MyClass assembly. This created a datasource entity. I changed the databinding of the textbox to this datasource; however, the expected result (that the textbox's contents would be "allen") was not achieved. Further, putting text into the textbox is not updating the name property of the object.

我知道我失去了一些东西基本在这里。在某些时候我应该要配合我,我表单代码的文本框里面初始化MyClass的类的实例,但是这并没有发生。一切我已经看了网上似乎掩盖使用数据绑定与对象(或我失踪完全标记),所以任何帮助是巨大的赞赏。

I know i'm missing something fundamental here. At some point i should have to tie my instance of the MyClass class that i initialized inside the form code to the textbox, but that hasn't occurred. Everything i've looked at online seems to gloss over using DataBinding with an object (or i'm missing the mark entirely), so any help is great appreciated.

编辑:

利用我所学到的答案,我看了看由Visual Studio生成的代码,它有以下内容:

Utilizing what I learned by the answers, I looked at the code generated by Visual Studio, it had the following:

this.myClassBindingSource.DataSource = typeof(BindingTest.MyClass);

如果我评论说出来,替补:

if I comment that out and substitute:

this.myClassBindingSource.DataSource = new MyClass();



我得到预期的行为。为什么由VS生成的默认代码喜欢它?假设这是比工作方法更正确,我应该怎么修改我的代码产生什么VS范围内工作?

I get the expected behavior. Why is the default code generated by VS like it is? Assuming this is more correct than the method that works, how should I modify my code to work within the bounds of what VS generated?

您必须将文本框的数据源作为新的数据源。但此外,您必须将数据源的数据源是你的类的实例

You must assign the textbox's data source to be your new datasource. But additionally, you must assign the datasource's datasource to be an instance of your class.

MyDataSource.DataSource = new MyClass();
TextBox1.DataSource = MyDataSource;

这应该适用于你的第一个通行证。正如其他人所说,你可能需要实现在你的类附加接口(INotifyPropertyChanged的等),如果你将要修改通过任何后台进程类的属性。

That should work for your first pass. As others have mentioned, you may need to implement additional interfaces on your class (INotifyPropertyChanged etc), if you are going to be modifying the class properties via any background processes.

如果你只通过形式更新的属性,那么就不需要这一步。

If you are only updating the properties via the form, then you do not need this step.