Winforms 将 ComboBox SelectedItem 绑定到对象属性 [英] Winforms Binding a ComboBox SelectedItem to an Object Property

查看:31
本文介绍了Winforms 将 ComboBox SelectedItem 绑定到对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个简单的类:

public class Customer
{
    public String CustomerID { get; set; }
    public String Forename { get; set; }
    public String Surname { get; set; }
}

public class Order
{
    public String OrderID { get; set; }
    public Decimal Value { get; set; }
    public Customer OrderedBy { get; set; }
}

然后我创建一个 Customer 对象列表:

I then create a list of Customer objects:

List<Customer> customers = new List<Customer>();
customers.Add(new Customer() { CustomerID = "1", Forename = "John", Surname = "Smith"});
customers.Add(new Customer() { CustomerID = "2", Forename = "Jeremy", Surname = "Smith" });

我有一个组合框,我将数据源设置为我的客户列表,并将 DisplayMember 设置为客户对象的 Forename 属性:

And I have a combo box, against which I set the data source to be my list of Customers, and the DisplayMember to be the Forename property of the Customer object:

comboBox1.DisplayMember = "Forename";
comboBox1.DataSource = customers;

结果是一个包含两个项目John"和Jeremy"的组合框.到现在我还没有太糊涂.

And the result is a combo box with two items, "John" and "Jeremy". Up to now I'm not too confused.

不过,我希望能够根据 Combobox 中的选择设置 Order 实例的OrderedBy"属性 - 复杂类型可以像这样绑定到 ComboBoxes 吗?

What I would like to be able to do though, is set the "OrderedBy" property of an instance of Order, based on the selection from the Combobox - Can complex types be bound to ComboBoxes like this?

我已经试过了,但它似乎没有更新 Order 实例的 OrderedBy 属性:

I've tried this, but it doesnt seem to be updating the OrderedBy property of the Order instance:

Order myOrder = new Order();
comboBox1.DataBindings.Add("SelectedItem", myOrder, "OrderedBy");

我不知道我正在尝试做的事情是否可行,或者它是否超出了 WinForms 中数据绑定的功能.

I dont know if what I'm trying to do is possible, or if it is beyond the capabilities of Data Binding in WinForms.

我想避免将我的 Order 对象更新为 ComboBox 上的事件处理程序的一部分,并尽可能仅使用数据绑定.

I'd like to avoid having to update my Order object as part of an event handler on the ComboBox, and solely use Data Binding if possible.

推荐答案

您的代码将更新有界对象的属性,但仅在 ComboBox 失去焦点之后.

Your code will update a property of the bounded object, but only after ComboBox loses focus.

如果您想在 SelectedItem 更改后立即更新属性,最快的方法是手动发送有关更改的消息.
ComboBox 中,当 SelectedItem 改变时,它会触发 SelectionChangesCommitted 事件.

If you want to update the property straight after the SelectedItem changed, the fastest approach will be to send a message about changes manually.
In a ComboBox, when the SelectedItemchanges, it will fire the SelectionChangesCommitted event.

您可以创建一个事件处理程序来处理更改并手动调用绑定:

You can create an event handler to take care of the change and manually call the binding:

private void combobox1_SelectionChangesCommitted(Object sender, EventArgs e)
{
    ((ComboBox)sender).DataBindings["SelectedItem"].WriteValue();
}

您还可以使用 ComboBox.ValueMember 属性并将您的对象属性绑定到 SelectedValue.

You could also use the ComboBox.ValueMember property and bind your object property to the SelectedValue.

这篇关于Winforms 将 ComboBox SelectedItem 绑定到对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆