我有这个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}";
}
}
}
关于数据绑定,我有两个问题:
>
当我在组合框中选择Mary时,两个TextBox控件不会更新。这是为什么?我做错了什么?
当我在组合框中更改文本“Mary”时,SelectedPerson对象不会从组合框中用新的名字更新,比如“Mary Changed”。如何实现更改ComboBox FirstName以更新所选人员的名字的行为?或者这是不可能的组合框?
如果我需要对问题补充更多细节,请告诉我。
您不需要SelectedPerson变量。看起来你只是连接了错误的数据源。用这种方法试试看:
textBoxFirstName.DataBindings.Add("Text", bindingSource, "FirstName");
textBoxLastName.DataBindings.Add("Text", bindingSource, "LastName");
试试这个
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;
}
您只需将ComboBox.DataSource
设置为List<;Person>
对象,该对象由此处的PersonList
属性表示。
向控件添加DataBinding
,当ComboBox从其DataSource
中选择新元素时,该控件需要更新:
textBoxFirstName.DataBindings.Add("Text", PersonList, "FirstName");
控件将自动更新。
在ComboBoxSelectedIndexChanged
处理程序中,可以将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;
}