.NET的WinForms组合框的BindingSource和数据绑定问题 [英] .NET WinForms combobox bindingsource and databinding question

查看:143
本文介绍了.NET的WinForms组合框的BindingSource和数据绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了下面的类来模拟人的:

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

    }
}

在我的主要形式,我有一个将被绑定到Person对象的数组组合框。当我从这个下拉框中选择一个人,我想显示自己的年龄(在NumericUpDown控件),是否有过敏(如一个复选框)和自己喜欢的颜色(在另一个组合框与DropDownStyle设置为DropDownList)。要做到这一点,我有:

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:

  1. 添加一个组合框(comboBoxPeople),NumericUpDown控件,一个复选框和另一个组合框(comboBoxFavoriteColor)到我的形式。
  2. 创建一个新的DataSource从我上面的声明Person类
  3. 添加一个BindingSource的我的表格
  4. BindingSource的的DataSource属性设置为#2中定义的数据源。
  5. 的数据源comboBoxPeople设置到BindingSource和的DisplayMember到BindingSource的名称属性
  6. 在我已绑定的BindingSource的时代属性NumericUpDown控件和BindingSource的对CheckBox控件的HasAllergies属性
  7. 在我的构造函数中,我创建了3人对象的数组,定义的所有属性,然后BindingSource的DataSource属性设置为这个数组

到目前为止,一切都按预期工作。现在,我想,以显示人最喜欢的颜色(即BindingSource的的FavoriteColor财产)comboBoxFavoriteColor。我已经设置了DropDownStyle为DropDownList,因为FavoriteColor是一个枚举。不过,我不清楚,我应该如何为它绑定ComboBox的,以1)包含FavoriteColor枚举值和2)具有相应的颜色设置为的SelectedItem当我选择一个人从comboBoxPeople。谁能给我一个这样的想法?非常感谢!

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!

推荐答案

您可以做汤姆建议,但有至少有一个简单的方法来做到这一点不改变 Person.FavoriteColor 为一个字符串。

You could do as Tom suggests, but there's at least one easy way to do it without changing Person.FavoriteColor to a string.

添加属性,以人名为 FavoriteColorString

public class Person 
{
    [...]
    public colorEnum FavoriteColor { get; set; }
    public string FavoriteColorString
    {
        get { return FavoriteColor.ToString(); }
        set { FavoriteColor = (colorEnum)Enum.Parse(typeof(colorEnum), value);  }
    }
}

重新编译使新属性显示在BindingSource的。

Recompile so the new property shows up in the bindingsource.

现在绑定 comboBoxFavoriteColor.SelectedItem FavoriteColorString

而在运行时,做汤姆说:

And at runtime, do as Tom said:

comboBoxFavoriteColor.DataSource = Enum.GetNames(typeof(colorEnum));

瞧!现在应该工作,你所希望的方式。

Voila! It should now work the way you want.

当你坚持下去的设置对象,只是没有坚持的FavoriteColorString财产。

When you persist the settings objects, just don't persist the FavoriteColorString property.

这篇关于.NET的WinForms组合框的BindingSource和数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆