简体   繁体   English

将对象属性的值绑定到 .NET WinForms 中的数据绑定 ComboBox

[英]Binding the value of an object's property to a data bound ComboBox in .NET WinForms

Currently building a WinForms data collection tool using C# and have been able to implement the binding of the values of my underling object\/entity's properties to controls on the my form, eg TextBoxes, MaskedTextBoxes and Checkboxes to properties on my domain model class (eg Person class).目前正在使用 C# 构建一个 WinForms 数据收集工具,并且已经能够将我的底层对象\/实体的属性的值绑定到我的表单上的控件,例如 TextBoxes、MaskedTextBoxes 和 Checkboxes 到我的域模型类上的属性(例如 Person班级)。 However, I have not been able to do this binding successfully with the ComboBox control.但是,我无法使用 ComboBox 控件成功完成此绑定。

With my Person class looking basically like this:我的 Person 类看起来基本上是这样的:

public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int? Gender { get; set; }
    }

ValueMember is a string and it does not change with a different selection in the combobox. ValueMember是一个字符串,它不会随着组合框中的不同选择而改变。 You need to use one of the Selected... properties.您需要使用Selected...属性之一。 Since it's an int, use SelectedValue .因为它是一个整数,所以使用SelectedValue

也许这会有所帮助:

cboGender.DataBindings.Add("SelectedIndex", genderValues, "Value", false, DataSourceUpdateMode.OnPropertyChanged);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM