.NET WinForms组合框绑定源和数据绑定问题
我创建了以下类来对人进行建模:
namespace DataBindingTest
{
public enum colorEnum
{
Red,
Green,
Yellow,
Blue,
}
class Person
{
private string _Name;
private int _Age;
private colorEnum _FavoriteColor;
private bool _HasAllergies;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public int Age
{
get { return _Age; }
set { _Age = value; }
}
public colorEnum FavoriteColor
{
get { return _FavoriteColor; }
set { _FavoriteColor = value; }
}
public bool HasAllergies
{
get { return _HasAllergies; }
set { _HasAllergies = value; }
}
}
}
在我的主窗体上,我有一个组合框,它将绑定到 Person 对象的数组。当我从此组合框中选择一个人时,我想显示他们的年龄(在 NumericUpDown 控件中)、他们是否过敏(作为复选框)以及他们最喜欢的颜色(在另一个 DropDownStyle 设置为 DropDownList 的组合框中)。为了实现这一点,我:
- 在我的表单中添加了一个组合框 (comboBoxPeople)、一个 NumericUpDown 控件、一个复选框和另一个组合框 (comboBoxFavoriteColor)。
- 从上面声明的 Person 类创建了一个新的 DataSource
- 将 BindingSource 添加到我的表单
- 将 BindingSource 的 DataSource 属性设置为 #2 中定义的 DataSource。
- 将comboBoxPeople的DataSource设置为BindingSource,将DisplayMember设置为BindingSource的Name属性
- 我已将BindingSource的Age属性绑定到NumericUpDown控件,将BindingSource的HasAllergies属性绑定到checkBox控件
- 在我的构造函数中,我创建了一个3 个 Person 对象的数组,定义了它们的所有属性,然后将 BindingSource 的 DataSource 属性设置为此数组
到目前为止,一切都按预期工作。现在我想在comboBoxFavoriteColor 中显示用户最喜欢的颜色(即BindingSource 的FavoriteColor 属性)。我已将 DropDownStyle 设置为 DropDownList,因为 favoriteColor 是一个枚举。但是,我不清楚应该如何绑定此组合框,以便它 1)包含FavoriteColor 枚举值,2)当我从comboBoxPeople 中选择一个人时,将适当的颜色设置为SelectedItem。谁能给我一个想法吗?非常感谢!
I created the following class to model a person:
namespace DataBindingTest
{
public enum colorEnum
{
Red,
Green,
Yellow,
Blue,
}
class Person
{
private string _Name;
private int _Age;
private colorEnum _FavoriteColor;
private bool _HasAllergies;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
public int Age
{
get { return _Age; }
set { _Age = value; }
}
public colorEnum FavoriteColor
{
get { return _FavoriteColor; }
set { _FavoriteColor = value; }
}
public bool HasAllergies
{
get { return _HasAllergies; }
set { _HasAllergies = value; }
}
}
}
On my main form, I have a combobox that will be bound to an array of Person objects. When I select a person from this combobox, I want to display their age (in a NumericUpDown control), whether they have allergies (as a checkbox) and their favorite color (in another combobox with DropDownStyle set to DropDownList). To accomplish this, I have:
- Added a comboBox (comboBoxPeople), a NumericUpDown control, a checkBox and another comboBox (comboBoxFavoriteColor) to my form.
- Created a new DataSource from my Person class declared above
- Added a BindingSource to my form
- Set the DataSource property of the BindingSource to the DataSource defined in #2.
- Set the DataSource for comboBoxPeople to the BindingSource and the DisplayMember to the Name property of the BindingSource
- I have bound the Age property of the BindingSource to a NumericUpDown control and the HasAllergies property of the BindingSource to a checkBox control
- In my constructor, I have created an array of 3 Person objects, defined all of their properties and then set the DataSource property of the BindingSource to this array
Thus far, everything is working as expected. Now I'd like to display the person's favorite color (i.e., the FavoriteColor property of the BindingSource) in comboBoxFavoriteColor. I've set the DropDownStyle to DropDownList since FavoriteColor is an enum. However, I'm unclear as to how I should bind this comboBox in order for it to 1) contain the FavoriteColor enum values and 2) have the appropriate color set as the SelectedItem when I choose a person from comboBoxPeople. Can anyone give me an idea on this? Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以按照 Tom 的建议进行操作,但至少有一种简单的方法可以做到这一点,而无需将
Person.FavoriteColor
更改为字符串。向 Person 添加一个名为
FavoriteColorString
的属性:重新编译,以便新属性显示在绑定源中。
现在将
comboBoxFavoriteColor.SelectedItem
绑定到FavoriteColorString
。在运行时,按照汤姆说的去做:
瞧!现在它应该按照您想要的方式工作。
当您保留设置对象时,请不要保留FavoriteColorString 属性。
You could do as Tom suggests, but there's at least one easy way to do it without changing
Person.FavoriteColor
to a string.Add a property to Person called
FavoriteColorString
:Recompile so the new property shows up in the bindingsource.
Now bind
comboBoxFavoriteColor.SelectedItem
toFavoriteColorString
.And at runtime, do as Tom said:
Voila! It should now work the way you want.
When you persist the settings objects, just don't persist the FavoriteColorString property.
您需要按照以下方式做一些事情:
..为了让事情变得更容易,只需将最喜欢的颜色存储为
string
类型,而不是colorEnum
You need to do something along these lines:
..and to make things easier, just store the favorite color as type
string
and not ascolorEnum