Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Xerx Avatar asked Sep 22 '08 15:09

Xerx


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 I enable data binding in Visual Studio?

Open a design surface in the editor and choose View > Data Sources. Add a data source to your project. Set the control that is created when you drag an item from the Data Sources window to the designer. Modify the list of controls that are associated with items in the Data Sources window.

How many types of DataBinding are there?

As mentioned earlier, one-way data binding in Angular can be of three types i.e Interpolation, Property binding, and Event binding.


2 Answers

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.

like image 94
ageektrapped Avatar answered Sep 24 '22 11:09

ageektrapped


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

editBox.DataBinding.Add("Text", car, "Name"); 
like image 22
Danimal Avatar answered Sep 22 '22 11:09

Danimal