简体   繁体   English

Winforms将ComboBox SelectedItem绑定到对象属性

[英]Winforms Binding a ComboBox SelectedItem to an Object Property

I have two simple classes: 我有两个简单的类:

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

and

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

I then create a list of Customer objects: 然后,我创建一个Customer对象列表:

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" });

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: 我有一个组合框,将数据源设置为我的客户列表,将其设置为DisplayMember作为Customer对象的Forename属性:

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

And the result is a combo box with two items, "John" and "Jeremy". 结果是一个组合框,其中包含两个项“ John”和“ Jeremy”。 Up to now I'm not too confused. 到目前为止,我还不太困惑。

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”属性-复杂类型可以这样绑定到ComboBox吗?

I've tried this, but it doesnt seem to be updating the OrderedBy property of the Order instance: 我已经尝试过了,但是它似乎没有更新Order实例的OrderedBy属性:

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

I dont know if what I'm trying to do is possible, or if it is beyond the capabilities of Data Binding in WinForms. 我不知道我想做的事情是否可行,或者它是否超出了WinForms中的数据绑定功能。

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上的Order对象作为事件处理程序的一部分进行更新,并尽可能使用Data Binding。

Your code will update a property of the bounded object, but only after ComboBox loses focus. 您的代码将更新有界对象的属性,但仅在ComboBox失去焦点之后。

If you want to update the property straight after the SelectedItem changed, the fastest approach will be to send a message about changes manually. 如果要在SelectedItem更改后立即更新属性,最快的方法是手动发送有关更改的消息。
In a ComboBox , when the SelectedItem changes, it will fire the SelectionChangesCommitted event. ComboBox ,当SelectedItem更改时,它将触发SelectionChangesCommitted事件。

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();
}

You could also use the ComboBox.ValueMember property and bind your object property to the SelectedValue . 您还可以使用ComboBox.ValueMember属性并将对象属性绑定到SelectedValue

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

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