Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

editBox.DataBinding.Add("Text", car, "Name");
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.

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.

Try:

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

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.

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.

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

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

it's

 this.editBox.DataBindings.Add(new Binding("Text", car, "Name"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top