Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding to an object in C#

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.

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";
    }
}

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.

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.

Edit:

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();

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?

like image 671
Allen Avatar asked Apr 03 '09 19:04

Allen


People also ask

What is data binding in C?

Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.

How do you bind an object in C#?

The INotifyPropertyChanged interface is used to notify a 'binding' that a property has changed, so the DataBinding infrastructure can act accordingly to it. Then, you can databind the MyName property to the Text Property of the textbox.

What are bindings C#?

When an object is assigned to an object variable of the specific type, then the C# compiler performs the binding with the help of . NET Framework. C# performs two different types of bindings which are: Early Binding or Static Binding. Late Binding or Dynamic Binding.


1 Answers

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;

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.

like image 128
Jason Coyne Avatar answered Oct 08 '22 02:10

Jason Coyne