[英]Binding WinForms control to ObjectA.ObjectB.Property
我有一個類代表這樣的車:
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級有四個輪胎,輪胎類看起來像這樣:
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中,我有一個與每個輪胎的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";
這很好用。 但我認為我現在遇到的問題是,我正試圖綁定一個不是汽車直接兒童財產的財產,而是汽車輪胎中的一個屬性。 所以,這樣的事情是行不通的:
comboBoxFrontLeftTimeSize.DataBindings.Add(new Binding("SelectedValue", bindingSourceForCars, "FrontLeftTire.Size", true, DataSourceUpdateMode.OnPropertyChanged));
我認為問題出在數據成員(“FrontLeftTire.Size”),但我不確定。 我只是以錯誤的方式引用這個,還是我在這里完全弄錯了?
我認為有兩個問題:
1)我認為您需要將Tire對象聲明為Properties,而不是Fields:
而不是這個:
public Tire FrontLeftTire = new Tire()
嘗試將其更改為:
private Tire frontLeftTire = new Tire()
public Tire FrontLeftTire {
get { return frontLeftTire; }
}
2)我認為你可能會遇到微軟在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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.