将WinForms控件绑定到ObjectA.ObjectB.Property

bmt*_*033 5 c# data-binding winforms

我有一个类代表这样的车:

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
    }
Run Code Online (Sandbox Code Playgroud)

如你所见,我的Car级有四个轮胎,轮胎类看起来像这样:

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
    }
Run Code Online (Sandbox Code Playgroud)

在我的WinForms UI中,我有一个与每个轮胎的Size属性相对应的组合框(下拉列表).我正在尝试将每个组合框绑定到适当轮胎的Size属性,但我用来绑定到car对象本身的属性的代码不起作用.这是我用来将组合框绑定到我汽车的Color属性的代码:

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";
Run Code Online (Sandbox Code Playgroud)

这很好用.但我认为我现在遇到的问题是,我正试图绑定一个不是汽车直接儿童财产的财产,而是汽车轮胎中的一个属性.所以,这样的事情是行不通的:

comboBoxFrontLeftTimeSize.DataBindings.Add(new Binding("SelectedValue", bindingSourceForCars, "FrontLeftTire.Size", true, DataSourceUpdateMode.OnPropertyChanged));
Run Code Online (Sandbox Code Playgroud)

我认为问题出在数据成员("FrontLeftTire.Size"),但我不确定.我只是以错误的方式引用这个,还是我在这里完全弄错了?

Lar*_*ech 6

我认为有两个问题:

1)我认为您需要将Tire对象声明为Properties,而不是Fields:

而不是这个:

public Tire FrontLeftTire = new Tire()
Run Code Online (Sandbox Code Playgroud)

尝试将其更改为:

private Tire frontLeftTire = new Tire()

public Tire FrontLeftTire {
  get { return frontLeftTire; }
}
Run Code Online (Sandbox Code Playgroud)

2)我认为你可能会遇到微软在4.0中对需要使用BindingSource的数据成员所做的重大改变.

而不是这个:

comboBoxFrontLeftTimeSize.DataBindings.Add(
                            new Binding("SelectedValue",
                                        bindingSourceForCars,
                                        "FrontLeftTire.Size",
                                        true,
                                        DataSourceUpdateMode.OnPropertyChanged));
Run Code Online (Sandbox Code Playgroud)

尝试将其更改为:

BindingSource bs = new BindingSource(bindingSourceForCars, null);
comboBoxFrontLeftTimeSize.DataBindings.Add(
                                       "SelectedValue",
                                       bs,
                                       "FrontLeftTire.Size",
                                       true,
                                       DataSourceUpdateMode.OnPropertyChanged));
Run Code Online (Sandbox Code Playgroud)