Binding object

T

Tony Johansson

Hello!

Here I use the Binding object to bind the text property of a TextBox control
txtName to the Name property of the product object

Product product = new Product("Apple", 17.19, 2);
Binding binding = new Binding("Text", product, "Name", true);
txtName.DataBindings.Add(binding);

The Binding object requires the following information:
- Name of the property of the control to which the binding is to done (e.g
Text Property
of a TextBox).
- Data source (the object product in the above example)
- The name of the property in the data source to which the property of the
control is to
be bound (Name in the above example, product.Name)

But I can also write in this way and skip the Binding object.
txtName.DataBinding.Add("Text", new Product("Apple", 17.19, 2), "Name");

Now to my question as you can see here I didn't use the Binding object at
all in the last example
so when is it nesessary to use this Binding object that I used in the first
example ?

//Tony
 
W

Willem van Rumpt

Product product = new Product("Apple", 17.19, 2);
Binding binding = new Binding("Text", product, "Name", true);
txtName.DataBindings.Add(binding);

But I can also write in this way and skip the Binding object.
txtName.DataBinding.Add("Text", new Product("Apple", 17.19, 2), "Name");

Now to my question as you can see here I didn't use the Binding object at
all in the last example
so when is it nesessary to use this Binding object that I used in the first
example ?

The latter version of Add constructs a Binding object for you from the
information provided, and returns it to you. If you're not going to use
the Binding object you can simply ignore the return value. So all in
all, it provides the same functionality, with less plumbing.

The end result is the same, only the road to it differs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top