Xerx
Xerx

Reputation: 3785

How can I do Databinding in c#?

I have the following class

public class Car
{
   public Name {get; set;}
}

and I want to bind this programmatically to a text box.

How do I do that?

Shooting in the dark:

...
Car car = new Car();
TextEdit editBox = new TextEdit();
editBox.DataBinding.Add("Name", car, "Car - Name");
...

I get the following error

"Cannot bind to the propery 'Name' on the target control.

What am I doing wrong and how should I be doing this? I am finding the databinding concept a bit difficult to grasp coming from web-development.

Upvotes: 36

Views: 36972

Answers (10)

itsmatt
itsmatt

Reputation: 31416

I believe that

editBox.DataBindings.Add(new Binding("Text", car, "Name"));

should do the trick. Didn't try it out, but I think that's the idea.

Upvotes: 3

Ramgy Borja
Ramgy Borja

Reputation: 2458

it's

 this.editBox.DataBindings.Add(new Binding("Text", car, "Name"));

Upvotes: 0

Graviton
Graviton

Reputation: 83306

Using C# 4.6 syntax:

editBox.DataBinding.Add(nameof(editBox.Text), car, nameof(car.Name));

if car is null, then the above code will fail in a more conspicuous way than using literal string to represent the datamember of car

Upvotes: 4

tofo
tofo

Reputation: 388

The following is generic class that can be used as a property and implements INotifyPropertyChanged used by bound controls to capture changes in the property value.

public class NotifyValue<datatype> : INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    datatype _value;
    public datatype Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Value"));
        }
    }

}

It can be declared like this:

public NotifyValue<int> myInteger = new NotifyValue<int>();

and assigned to a textbox like this

Textbox1.DataBindings.Add(
    "Text", 
    this, 
    "myInteger.Value", 
    false, 
    DataSourceUpdateMode.OnPropertyChanged
);

..where "Text" is the property of the textbox, 'this' is current Form instance.

A class does not have to inherit the INotifyPropertyChanged class. Once you declare an event of type System.ComponentModel.PropertyChangedEventHandler the class change event will be subscribed to by the controls databinder

Upvotes: 1

Danimal
Danimal

Reputation: 7710

Without looking at the syntax, I'm pretty sure it's:

editBox.DataBinding.Add("Text", car, "Name");

Upvotes: 11

ageektrapped
ageektrapped

Reputation: 14562

You want

editBox.DataBindings.Add("Text", car, "Name");

The first parameter is the name of the property on the control that you want to be databound, the second is the data source, the third parameter is the property on the data source that you want to bind to.

Upvotes: 53

John Hunter
John Hunter

Reputation: 4142

You are quite close the data bindings line would be

editBox.DataBinding.Add("Text", car, "Name");

This first parameter is the property of your editbox object that will be data bound. The second parameter is the data source you are binding to and the last parameter is the property on the data source that you want to bind to.

Bear in mind that the data binding is one way so if you change the edit box then the car object gets updated but if you change the car name directly the edit box is not updated.

Upvotes: 6

Romain Verdier
Romain Verdier

Reputation: 13011

editBox.DataBinding.Add("Text", car, "Name");

First arg is the name of the control property, the second is the object to bind, and the last, the name of the object property you want to use as the data source.

Upvotes: 7

TcKs
TcKs

Reputation: 26642

Try:

editBox.DataBinding.Add( "Text", car", "Name" );

Upvotes: 3

user1228
user1228

Reputation:

You're trying to bind to the "Name" of the TextEdit control. The name is used for accessing the control programmatically, and cannot be bound against. You should be binding against the Text of the control.

Upvotes: 2

Related Questions