views:

1502

answers:

3

I need some help/guidance on WinForms data binding and I can't seem to get Google to help me with this one.

Here is my scenario. Consider the following classes which is similar to what I need:

public class Car
{
    public string Name { get; set; }
    public List<Tire> Tires { get; set; }
}

public class Tire
{
    public double Pressure { get; set; }
}

My instances of this will be an object of class Car with a List with four Tire objects. Note that I will always have a known number of objects in the list here.

Now I want to data bind this to a Form containing five textboxes. One textbox with the name of the car and one textbox with each of the tires pressures.

Any idea on how to make this work? The designer in VS does not seem to allow me to set this up by assigning to list indexes like Tires[0].Pressure.

My current solution is to bind to a "BindableCar" which would be like:

public class BindableCar
{
    private Car _car;

    public BindableCar(Car car)
    {
        _car = car;
    }

    public string Name
    {
        get { return _car.Name; }
        set { _car.Name = value; }
    }

    public double Tire1Pressure
    {
        get { return _car.Tires[0].Pressure; }
        set { _car.Tires[0].Pressure = value; }
    }

    public double Tire2Pressure
    {
        get { return _car.Tires[1].Pressure; }
        set { _car.Tires[1].Pressure = value; }
    }

    public double Tire3Pressure
    {
        get { return _car.Tires[2].Pressure; }
        set { _car.Tires[2].Pressure = value; }
    }

    public double Tire4Pressure
    {
        get { return _car.Tires[3].Pressure; }
        set { _car.Tires[3].Pressure = value; }
    }
}

but this becomes really ugly when my lists contains 20 instead of 4 objects, and for each of those objects I want to bind against 6 properties. That makes a huge "BindableObject"!

+2  A: 

While the WinForms designer may not let you do this, have you tried setting up the binding in code? I imagine there is no problem binding a textbox to someCar.Tires[1].Pressure.

Judah Himango
No, I haven't as I assumed that this was not supported due to the message prompted by the designer. I'll try this as soon as possible.
Geir-Tore Lindsve
A: 

You should be able to bind to a list via code instead of the designer. Here's an example (from just a few hours ago).

Jess
+10  A: 

You should note that you can bind controls to any object type that implements the interfaces IList, ICollection or IEnumerable or inherits from classes that implement these interfaces. Generic collections also qualify for this kind of binding.

These are internally converted to an IBindingList instance.

Check out the following links for more information:

  1. Roadmap for Windowsforms databinding - Takes a very comprehensive look at the implementation and possibilities and provides a variety of links to other KB articles.
  2. Winforms Object binding - Rockford Lhotka's article on the subject. Provides a Windows forms designer-oriented way of implementing databinding.
  3. Databinding with Windows forms 2.0 - This book by Brian Noyes explores various aspects of Databinding, both in complex and simple scenarios.
Cerebrus
Thanks for the great links!
Geir-Tore Lindsve
My pleasure! ;-)
Cerebrus