组合框绑定源业务对象 [英] Combobox bindingsource business-objects

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

问题描述

有人能比我多喝咖啡因吗?请看看这个例子,告诉我我做错了什么吗?

我有一个名为Customer的自定义业务对象,以及一个具有BindingSource1,NumericUpDown1,TextBox1和ComboBox1以及附加代码的Form1.

在属性集中"中设置断点表明,即使NumericUpDown和TextBox的集合像疯了一样,ComboBox的集合也不会触发.

我显然错过了一些东西,我也不知道.

谢谢

Can someone else who has had more caffeine than I please look at this example and tell me what I am doing wrong?

I have a custom business object called Customer and a Form1 with a BindingSource1, NumericUpDown1, TextBox1 and ComboBox1 and the attached code.

Setting break points in the Property Sets shows that the Set for the ComboBox never fires at all even though the Set for the NumericUpDown and TextBox fire like crazy.

I''ve obviously missed something and I know not what.

Thanks

Option Explicit On
Option Strict On
Public Class Customer
Public Enum ChildGroup
Infant
Toddler
Preschool
GradeSchool
HighSchool
College
End Enum

Private m_Id As Int32
Private m_Name As String
Private m_Child As ChildGroup

Public Sub New(ByVal id As Int32, ByVal name As String, ByVal child As ChildGroup)
m_Id = id
m_Name = name
m_Child = child
End Sub

Public Property Id() As Int32
Get
Return m_Id
End Get
Set(ByVal value As Int32)
m_Id = value
End Set
End Property

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property

Public Property Child() As ChildGroup
Get
Return m_Child
End Get
Set(ByVal value As ChildGroup)
m_Child = value
End Set
End Property
End Class

Option Explicit On
Option Strict On
Public Class Form1

Private m_Customer As New Customer(123, "John Doe", Customer.ChildGroup.HighSchool)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindingSource1.DataSource = Customer
NumericUpDown1.Minimum = 0
NumericUpDown1.Maximum = 10000
NumericUpDown1.DataBindings.Add( _
New Windows.Forms.Binding("Value", BindingSource1, "Id", False, _ DataSourceUpdateMode.OnPropertyChanged))
TextBox1.DataBindings.Add( _
New Windows.Forms.Binding("Text", BindingSource1, "Name", False, _ DataSourceUpdateMode.OnPropertyChanged))
ComboBox1.DataBindings.Add( _
New Windows.Forms.Binding("Text", BindingSource1, "Child", False, _ DataSourceUpdateMode.OnPropertyChanged))
ComboBox1.Items.Add(Customer.ChildGroup.Infant)
ComboBox1.Items.Add(Customer.ChildGroup.Toddler)
ComboBox1.Items.Add(Customer.ChildGroup.Preschool)
ComboBox1.Items.Add(Customer.ChildGroup.GradeSchool)
ComboBox1.Items.Add(Customer.ChildGroup.HighSchool)
ComboBox1.Items.Add(Customer.ChildGroup.College)
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.DisplayMember = "Child"
ComboBox1.ValueMember = "Child"
End Sub

Public ReadOnly Property Customer() As Customer
Get
Return m_Customer
End Get
End Property
End Class

推荐答案

添加数据绑定时,可以使用ComboBox的SelectedItem属性代替Text属性.然后,安装员将在ComboBox丢失的焦点上触发.

You can use the SelectedItem property instead of the Text property of the ComboBox when you are adding the data bindings. Then the setter will fire on the lost focus of the ComboBox.

ComboBox1.DataBindings.Add( _
        New Windows.Forms.Binding("SelectedItem", BindingSource1, "Child", False, _
                                  DataSourceUpdateMode.OnPropertyChanged))


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

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