1

由于命名和枚举之间的差异,我不能简单地绑定。想到以下几点:

  • 使用翻译值数组作为数据源;
  • 使用格式/解析事件对属性进行数据绑定,以在枚举值和显示值之间进行转换。

例子:

BindingSource bs = new BindingSource();
bs.DataSource = this.testdata;

this.textBox1.DataBindings.Add("Text", this.bs, "CurrentState", true, DataSourceUpdateMode.OnPropertyChanged);
this.textBox1.ReadOnly = true;
this.comboBox1.DataSource = new List<string>() { "Goed", "Slecht", "Lelijk", "-" };
Binding b = new Binding("SelectedValue", this.bs, "CurrentState", true, DataSourceUpdateMode.OnPropertyChanged);
b.Parse += new ConvertEventHandler(TextToState);
b.Format += new ConvertEventHandler(StateToText);
this.comboBox1.DataBindings.Add(b);

[...]

   void StateToText(object sender, ConvertEventArgs e)
    {
        State state = (State)Enum.Parse(typeof(State), e.Value as string);

        switch (state)
        {
            case State.Good:
                e.Value = "Goed";
                break;
            case State.Bad:
                e.Value = "Slecht";
                break;
            case State.Ugly:
                e.Value = "Lelijk";
                break;
            default:
                e.Value = "-";
                break;
        }
    }

    void TextToState(object sender, ConvertEventArgs e)
    {
        switch (e.Value as string)
        {
            case "Goed":
                e.Value = State.Good;
                break;
            case "Slecht":
                e.Value = State.Bad;
                break;
            case "Lelijk":
                e.Value = State.Ugly;
                break;
            default:
                e.Value = State.None;
                break;
        }
    }

这只是一个测试功能的示例。文本框用于验证组合框中显示的值是否真的是数据绑定属性的值。

此代码有效。但是有两个问题我无法解决:

  • 组合框选择数据源中的第一个值(不是属性的状态),直到所选项目被更改一次。然后绑定工作正常。
  • 我无法关闭表单。

我不明白为什么绑定在加载时无法更新。我尝试重置绑定等,但没有任何效果。我也不知道为什么表格被阻止关闭。

4

1 回答 1

2

为什么不考虑编写一个包含要使用的键/值对的类?这是一个小示例实现,假设您有一个名为 combobox1 的组合框。

namespace WindowsFormsApplication1
{

   public enum State {None, Good, Bad, Ugly }

    public partial class Form1 : Form
    {
        List<StateTextPair> pairs;

        public Form1()
        {
            InitializeComponent();
            InitializeList();
            this.comboBox1.DataSource = pairs;
            this.comboBox1.ValueMember = "State";
            this.comboBox1.DisplayMember = "Name";
            this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
            ComboBox box = (ComboBox)sender;
            StateTextPair selected = (StateTextPair)box.SelectedItem;
            switch (selected.State) {
                case State.None:
                    // do nothing
                    break;
                default:
                    MessageBox.Show(selected.State.ToString() + " == " + selected.Name);
                    break;
            }
        }

        private void InitializeList() {
            pairs = new List<StateTextPair>();
            pairs.Add(new StateTextPair(State.None, " - Please select a value - "));
            pairs.Add(new StateTextPair(State.Good, "Goed"));
            pairs.Add(new StateTextPair(State.Bad, "Slecht"));
            pairs.Add(new StateTextPair(State.Ugly, "Lelijk"));
        }
    }

    public class StateTextPair {
        private string name;
        private State state;

        public State State { get { return state; } }
        public string Name { get { return name; } }

        public StateTextPair(State s, string n)
        {
            this.name = n;
            this.state = s;
        }
    }
}
于 2013-03-10T01:03:29.480 回答