的WinForms绑定组合框的SelectedItem到一个对象的属性 [英] Winforms Binding a ComboBox SelectedItem to an Object Property

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

问题描述

这是我在数据绑定的第一次尝试中的WinForms,所以请与我裸露

This is my first attempt at Databinding in WinForms, so please bare with me.

我有两个简单的类:

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是Customer对象的属性用的名字:

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;

和结果是两个项目的组合框,约翰和杰里米。到现在为止我不是太糊涂了。

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

我想什么能够做虽然,是集阶的一个实例的OrderedBy属性,基于从组合框中选择 - 可以复杂类型绑定到这样的组合框

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?

我已经试过这一点,但它似乎没有被更新的的OrderedBy财产? Order实例:

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.

我想,以避免更新我的订单对象的组合框的事件处理程序的一部分,并且只使用数据绑定如果可能的话

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.

干杯

推荐答案

您的代码会更新的属性,但只有经过组合框界对象失去焦点。

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

如果你想后直的SelectedItem 更新属性改变 - 最快的方式将手动发送的消息约为变化<。 BR>
组合框 的SelectedItem 将触发事件 SelectionChangesCommitted 。结果
所以,你可以创建从那里发送有关信息的变化

If you want update property straight after SelectedItem changed - fastest approach will be to send message about changes manually.
In ComboBox changing of SelectedItem will fire event SelectionChangesCommitted.
So you can create event handler from where send message about changes

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

或使用使用 ComboBox.ValueMember 属性并绑定你的对象属性为的SelectedValue

Or using use ComboBox.ValueMember property and bound your object property to the SelectedValue

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

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