Binding WinForms to an ObjectA.ObjectB.Property

I have a class that represents such a car:

public class Car
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }

    public enum Colors
    {
        LaserRed,
        GenuineGraniteMica,
        BluePearl,
        SandMicaMetallic,
        NightArmorMetallic
    }

    private string _make;
    public string Make
    {
        get { return _make; }
        set {
                _make = value;
                RaisePropertyChanged();
            }
    }

    private string _model;
    public string Model
    {
        get { return _model; }
        set {
                _model = value;
                RaisePropertyChanged();
            }
    }

    private Colors _color;
    public Colors Color
    {
        get { return _color; }
        set {
                _color = value;
                RaisePropertyChanged();
            }
    }

    public Tire FrontLeftTire = new Tire();
    public Tire FrontRightTire = new Tire();
    public Tire RearLeftTire = new Tire();
    public Tire RearRightTire = new Tire();

    public Car()
    {
        // initialization code
    }

As you can see, my Car class has four tires, and the Tire class is as follows:

public class Tire : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }

    public enum SizeValues
    {
        [Description("17 inches")]
        SeventeenInches,
        [Description("18 inches")]
        EighteenInches,
        [Description("19 inches")]
        NineteenInches,
        [Description("20 inches")]
        TwentyInches,
    }

    private string _brand;
    public string Brand
    {
        get { return _brand; }
        set
        {
            _brand = value;
            RaisePropertyChanged();
        }
    }

    private SizeValues _size;
    public SizeValues Size
    {
        get { return _size; }
        set
        {
            _size = value;
            RaisePropertyChanged();
        }
    }

    public Tire()
    {
        // initialization code
    }

In my WinForms user interface, I have a combobox (dropdownlist) that matches the Size property of each bus. I try to associate each combobox with the corresponding property of the bus, but the code that I used to bind to the properties of the car object itself does not work. Here is the code I'm using to bind combobox to the Color property of my car:

comboBoxCarColor.DataBindings.Add(new Binding("SelectedValue", bindingSourceForCars, "Color", true, DataSourceUpdateMode.OnPropertyChanged));
comboBoxCarColor.DataSource = new BindingSource(Utility.ConvertEnumToListOfKeyValuePairs(typeof(Car.Color)), null);
comboBoxCarColor.DisplayMember = "Value";
comboBoxCarColor.ValueMember = "Key";

. , , , , , , . , - :

comboBoxFrontLeftTimeSize.DataBindings.Add(new Binding("SelectedValue", bindingSourceForCars, "FrontLeftTire.Size", true, DataSourceUpdateMode.OnPropertyChanged));

, ( "FrontLeftTire.Size" ), . ?

+5
1

, :

1) , Tire "", "":

:

public Tire FrontLeftTire = new Tire()

:

private Tire frontLeftTire = new Tire()

public Tire FrontLeftTire {
  get { return frontLeftTire; }
}

2) , , Microsoft 4.0 , BindingSource.

:

comboBoxFrontLeftTimeSize.DataBindings.Add(
                            new Binding("SelectedValue",
                                        bindingSourceForCars,
                                        "FrontLeftTire.Size",
                                        true,
                                        DataSourceUpdateMode.OnPropertyChanged));

:

BindingSource bs = new BindingSource(bindingSourceForCars, null);
comboBoxFrontLeftTimeSize.DataBindings.Add(
                                       "SelectedValue",
                                       bs,
                                       "FrontLeftTire.Size",
                                       true,
                                       DataSourceUpdateMode.OnPropertyChanged));
+4

All Articles