如何将WinForms ComboBox的SelectedItem数据绑定到TextBox

时间:2019-01-08 21:25:55

标签: c# winforms combobox textbox 2-way-object-databinding

代码

我有这个用户界面

Screenshot of UI

使用此代码

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;

namespace WinFormsComboBoxDatabinding
{
    public partial class Form1 : Form
    {
        public List<Person> PersonList { get; set; }
        public Person SelectedPerson { get; set; }

        public Form1()
        {
            InitializeComponent();
            InitializePersonList();
            InitializeDataBinding();
        }

        private void InitializePersonList()
        {
            PersonList = new List<Person>
            {
                new Person { FirstName = "Bob", LastName = "Builder" },
                new Person { FirstName = "Mary", LastName = "Poppins" }
            };
        }

        private void InitializeDataBinding()
        {
            SelectedPerson = PersonList[0];

            var bindingSource = new BindingSource();
            bindingSource.DataSource = PersonList;

            comboBox.DisplayMember = "FirstName";
            //comboBox.ValueMember = "LastName";
            comboBox.DataSource = bindingSource;

            textBoxFirstName.DataBindings.Add("Text", SelectedPerson, "FirstName");
            textBoxLastName.DataBindings.Add("Text", SelectedPerson, "LastName");
        }

        private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedPerson = comboBox.SelectedItem as Person;

            Debug.WriteLine($"SelectedPerson: {SelectedPerson}");
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return $"{FirstName} {LastName}";
        }
    }
}

问题

关于数据绑定,我有两个问题:

  1. 当我在ComboBox中选择Mary时,两个TextBox控件都不会更新。这是为什么?我做错了什么?

    Screenshot of UI

    Screenshot of Debug output

  2. 当我在ComboBox中更改文本“ Mary”时,SelectedPerson对象不会从ComboBox中更新为新的FirstName,例如“ Mary更改”。我如何实现更改ComboBox FirstName以更新SelectedPerson的FirstName的行为?还是使用ComboBox无法做到?

    UI of Mary changed

    Debug output of Mary changed

其他实验

  • 我已经看到,可以在调用comboBox_SelectedIndexChanged时设置两个TextBox控件的Text属性,但这并不是真正的数据绑定。那将是手动执行所有更新逻辑。

让我知道是否需要在问题中添加更多详细信息。

3 个答案:

答案 0 :(得分:0)

您不需要SelectedPerson变量。看来您只是连接了错误的数据源。尝试这种方式:

textBoxFirstName.DataBindings.Add("Text", bindingSource, "FirstName");
textBoxLastName.DataBindings.Add("Text", bindingSource, "LastName");

答案 1 :(得分:0)

您只需要将ComboBox.DataSource设置为List<Person>对象,在这里由PersonList属性表示。
当ComboBox从其DataBinding中选择一个新元素时,将DataSource添加到需要更新的控件中:

textBoxFirstName.DataBindings.Add("Text", PersonList, "FirstName");

控件会自动更新。
在ComboBox SelectedIndexChanged处理程序中,可以将SelectedPerson属性值设置为当前SelectedItem,并将其强制转换为Person类。

public List<Person> PersonList { get; set; }
public Person SelectedPerson { get; set; }

private void InitializePersonList()
{
    this.PersonList = new List<Person>
    {
        new Person { FirstName = "Bob", LastName = "Builder" },
        new Person { FirstName = "Mary", LastName = "Poppins" }
    };
}

private void InitializeDataBinding()
{
    comboBox.DisplayMember = "FirstName";
    comboBox.DataSource = this.PersonList;

    textBoxFirstName.DataBindings.Add("Text", PersonList, "FirstName");
    textBoxLastName.DataBindings.Add("Text", PersonList, "LastName");
}

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    this.SelectedPerson = (Person)(sender as ComboBox).SelectedItem;
}

答案 2 :(得分:0)

尝试一下

private void InitializeDataBinding()
        {
            SelectedPerson = PersonList[0];

            var bindingSource = new BindingSource();
            bindingSource.DataSource = PersonList;

            comboBox.DisplayMember = "FirstName";
            comboBox.DataSource = bindingSource;

            textBoxFirstName.DataBindings.Add("Text", bindingSource, "FirstName");
            textBoxLastName.DataBindings.Add("Text", bindingSource, "LastName");
        }

private void comboBox_TextChanged(object sender, EventArgs e)
        {
            var selectedPerson = PersonList.FirstOrDefault(x => x.FirstName == comboBox.Text);
            if (selectedPerson == null) return;
            comboBox.SelectedItem = selectedPerson;
        }