gpt4 book ai didi

c# - 将 WinForms 控件绑定(bind)到 ObjectA.ObjectB.Property

转载 作者:太空狗 更新时间:2023-10-29 23:08:55 26 4
gpt4 key购买 nike

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

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
}

如您所见,我的 Car 类有四个轮胎,Tire 类如下所示:

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
}

在我的 WinForms UI 中,我有一个组合框(下拉列表)对应于每个轮胎的尺寸属性。我正在尝试将每个组合框绑定(bind)到适当轮胎的 Size 属性,但我一直用来绑定(bind)到汽车对象本身属性的代码不起作用。下面是我用来将组合框绑定(bind)到我的汽车的 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";

这很好用。但我认为我现在遇到的问题是我试图绑定(bind)一个不是汽车的直接子属性而是汽车轮胎之一的属性。所以,这样的事情是行不通的:

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

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

最佳答案

我认为有两个问题:

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));

关于c# - 将 WinForms 控件绑定(bind)到 ObjectA.ObjectB.Property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14607572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com