Binding not writing to datasource on .NET Compact Framework Form -- works on Full Framework

Posted by Dave Welling on Stack Overflow See other posts from Stack Overflow or by Dave Welling
Published on 2010-03-27T21:37:32Z Indexed on 2010/03/27 21:43 UTC
Read the original article Hit count: 793

Filed under:
|
|

I have a problem with a bound user control writing back to it's datasource on a NetCF forms application. The application is too complex to post code, so I made a toy version to show you.

  • I create a form, usercontrol with a combobox, a class (testBind) and another class (TestLookup).
  • I bind a property of the usercontrol ("value") to a property ("selectedValue") on the testBind class. The testBind class implements INotifyPropertyChanged.
  • I create a few fascade methods on the user control to bind the contained combobox to a BindingList(of TestLookup).
  • I create a button to show the value of the testBind bound property (in a MessageBox).

The messagebox returns "-1" every time regardless of the combobox entry selected. I can take the EXACT same code, paste it in a full framework Forms app and it will return the correct value of the selected combobox entry.

Imports System.ComponentModel

Public Class Form2 Inherits Form

Private _testBind1 As testBind
Private _testUserControlX As UserControlX
Friend WithEvents _buttonX As System.Windows.Forms.Button
Public Sub New()
    _buttonX = New System.Windows.Forms.Button
    _buttonX.Location = New System.Drawing.Point(126, 228)
    _buttonX.Size = New System.Drawing.Size(70, 21)
    _testBind1 = New testBind
    _testUserControlX = New UserControlX()

    Dim _lookup As New System.ComponentModel.BindingList(Of TestLookup)()
    _lookup.Add(New TestLookup(1, "text1"))
    _lookup.Add(New TestLookup(2, "text2"))

    _testUserControlX.DataSource = _lookup
    _testUserControlX.DisplayMember = "Text"
    _testUserControlX.ValueMember = "ID"
    _testUserControlX.DataBindings.Add("Value", _testBind1, "SelectedID", False, DataSourceUpdateMode.OnValidation)

    MinimizeBox = False

    Controls.Add(_testUserControlX)
    Controls.Add(_buttonX)

End Sub

Private Sub ButtonX_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _buttonX.Click
    MessageBox.Show(_testBind1.SelectedID.ToString())
End Sub


Public Class testBind
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _selectedRow As Integer = -1

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Sub OnPropertyChanged(ByVal PropertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
    End Sub

    Public Property SelectedID() As Integer
        Get
            Return _selectedRow
        End Get
        Set(ByVal value As Integer)
            _selectedRow = value
            OnPropertyChanged("SelectedID")
        End Set
    End Property
End Class

Public Class TestLookup
    Private _text As String
    Private _id As Integer
    Public Sub New(ByVal id As Integer, ByVal text As String)
        _text = text
        _id = id
    End Sub
    Public Property ID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property
    Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property
End Class

End Class

Public Class UserControlX Inherits System.Windows.Forms.UserControl

Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Public Sub New()
    Me.ComboBox1 = New System.Windows.Forms.ComboBox
    Me.Controls.Add(Me.ComboBox1)
End Sub

Public Property Value() As Integer
    Get
        Return ComboBox1.SelectedValue
    End Get
    Set(ByVal value As Integer)
        ComboBox1.SelectedValue = value
    End Set
End Property


Public Property DataSource() As Object
    Get
        Return ComboBox1.DataSource
    End Get
    Set(ByVal value As Object)
        ComboBox1.DataSource = value
    End Set
End Property


Public Property ValueMember() As String
    Get
        Return ComboBox1.ValueMember
    End Get
    Set(ByVal value As String)
        ComboBox1.ValueMember = value
    End Set
End Property


Public Property DisplayMember() As String
    Get
        Return ComboBox1.DisplayMember
    End Get
    Set(ByVal value As String)
        ComboBox1.DisplayMember = value
    End Set
End Property

End Class

© Stack Overflow or respective owner

Related posts about .netcf

Related posts about binding