我错过了什么...窗体与亚音速结合

时间:2009-05-07 17:51:32

标签: winforms data-binding subsonic

我有一个带有列表框和几个文本框的表单,当用户从列表框中选择一个项目时,我需要在txt框中显示适当的信息并允许用户编辑它。

这是我的表单加载事件:

Private prt As New DataAccess.Part

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim lc As System.Web.UI.WebControls.ListItemCollection = DataAccess.Part.GetListItems()
        cboParts.DataSource = lc
        lstParts.DataSource = lc

        txtPartBefore.DataBindings.Add(New Binding("Text", prt, "PartNumBefore", True, DataSourceUpdateMode.OnPropertyChanged))
        txtPartAfter.DataBindings.Add(New Binding("Text", prt, "PartNumAfter", True, DataSourceUpdateMode.OnPropertyChanged))
        txtOperation.DataBindings.Add(New Binding("Text", prt, "Operation", True, DataSourceUpdateMode.OnPropertyChanged))
        txtNotes.DataBindings.Add(New Binding("Text", prt, "Notes", True, DataSourceUpdateMode.OnPropertyChanged))

End Sub

列表框的SelectedIndexChange:

Private Sub lstParts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstParts.SelectedIndexChanged

    Dim ctl As ListControl = DirectCast(sender, ListControl)

    prt = DataAccess.Part.FetchByID(ctl.SelectedValue.value)

End Sub

我正在使用VB.Net 2005,但如果需要,可以从C#转换您的建议;)

由于 Tony W

3 个答案:

答案 0 :(得分:1)

您实际上并未将新选择的值绑定到文本框。您所做的只是将引用(prt)更改为从原始绑定对象指向用户刚刚选择的其他对象。

你需要做这样的事情:

    Private Sub lstParts_SelectedIndexChanged(
        ByVal sender As System.Object, ByVal e As System.EventArgs) 
        Handles lstParts.SelectedIndexChanged

        Dim ctl As ListControl = DirectCast(sender, ListControl)

        prt = DataAccess.Part.FetchByID(ctl.SelectedValue.value)

        BindControls(prt)
    End Sub

    Sub BindControls(ByVal newPart as DataAccess.Part)
        txtPartBefore.DataBindings.Clear()
        txtPartAfter.DataBindings.Clear()
        txtOperation.DataBindings.Clear()
        txtNotes.DataBindings.Clear()

        txtPartBefore.DataBindings.Add(
            New Binding("Text", newPart, "PartNumBefore", True, 
            DataSourceUpdateMode.OnPropertyChanged))
        txtPartAfter.DataBindings.Add(
            New Binding("Text", newPart, "PartNumAfter", True, 
            DataSourceUpdateMode.OnPropertyChanged))
        txtOperation.DataBindings.Add(
            New Binding("Text", newPart, "Operation", True, 
            DataSourceUpdateMode.OnPropertyChanged))
        txtNotes.DataBindings.Add(
            New Binding("Text", newPart, "Notes", True, 
            DataSourceUpdateMode.OnPropertyChanged))
    End Sub

答案 1 :(得分:0)

您的问题是您在代码中分配prt这一事实。您的控件绑定到创建绑定时的任何引用prt点。稍后分配时,控件不会指向它。

您应该做的是添加BindingSource组件,让它指向prt所属的对象类型,然后将您的控件绑定到BindingSource组件。需要检索时,将DataSource的{​​{1}}属性设置为检索对象。

答案 2 :(得分:0)

您将绑定到属性'Prt':

public DataAccess.Part Prt { get { return prt; } set { prt = value; } }

Public Property Prt() As DataAccess.Part
Get
  Return prt
End Get
Set(ByVal Value As DataAccess.Part)
  prt = value
End Set
End Property