J'ai cette interface utilisateur
avec ce code
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}"; } } }
Faites-moi savoir si j'ai besoin d'ajouter plus de détails à la question.
3 Réponses :
Vous n'avez pas besoin de la variable SelectedPerson. Il semble que vous ayez simplement câblé la mauvaise DataSource. Essayez-le de cette façon:
textBoxFirstName.DataBindings.Add("Text", bindingSource, "FirstName"); textBoxLastName.DataBindings.Add("Text", bindingSource, "LastName");
En effet, cela a résolu le problème de la question 1 et la mise à jour du prénom dans la zone de texte met à jour la zone de liste déroulante. Merci! Des idées sur la question 2?
@Lernkurve Si vous modifiez la valeur du nom de la zone de texte, cela fonctionne (lorsque la zone de texte perd le focus, il met à jour la propriété). Le faire à partir de la ComboBox est un endroit un peu étrange pour le faire.
Il vous suffit de définir ComboBox.DataSource
sur l'objet List
, représenté par la propriété PersonList
ici.
Ajoutez un DataBinding
aux contrôles qui doivent être mis à jour lorsque le ComboBox sélectionne un nouvel élément dans sa DataSource
:
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; }
Les contrôles sont mis à jour automatiquement.
Dans le gestionnaire ComboBox SelectedIndexChanged
, vous pouvez définir la valeur de la propriété SelectedPerson
sur le SelectedItem
actuel, en le convertissant en Person code > classe.
textBoxFirstName.DataBindings.Add("Text", PersonList, "FirstName");
Essayez ceci
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; }