How to do two way data binding in VB.NET Winforms with View Model

+2 votes
asked Jun 9, 2013 in Winforms by Patrik
retagged Jun 9, 2013

I am trying to implement MVP-VM design pattern in VB.NET winforms. But when I bind my form control with view model properties using below code, when there is a change in the textbox text, changes are not reflected into View model properties. Is it possible to do two-way binding in winforms? If so, what is the best way to do that?

Public Class ContactInfoViewModel

    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property

    Private _lastName As String
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property
End Class

In ContactInfo Winforms I have below code

Public Sub BindControls()
    txtFirstName.DataBindings.Add("Text", _contactInfoViewModel, "FirstName")
    txtLastName.DataBindings.Add("Text", _contactInfoViewModel, "LastName")
End Sub

I am calling this BindControls() method from the form constructor.

Share

1 Answer

+2 votes
answered Jun 10, 2013 by Bibek
selected Jun 10, 2013 by administrator
 
Best answer

Two way bind can be implemented by using 2 modifications on your code
1. View Model must implement INotifyPropertyChanged interface.
2. Use overloaded method of Add method in DataBindings to listen to PropertyChanged event of View Model.

First modification:
INotifyPropertyChanged interface has "PropertyChanged" event. So, your view model has to raise "PropertyChanged" event whenever there is a change in the property.

Below is the sample code

Public Class ContactInfoViewModel
Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
           OnPropertyChanged("FirstName")
        End Set
    End Property

    Private _lastName As String
    Public Property LastName() As String
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
           OnPropertyChanged("LastName")
        End Set
    End Property

  Private Sub OnPropertyChanged(ByVal propertyName As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
  End Sub
End Class

Second Change:
Use the below overloaded method for DataBindings.Add to listen to the Property Changed event.

txtFirstName.DataBindings.Add("Text", _customerContactInfoViewModel.CurrentCustomerContactInfo, "FirstName", True, **DataSourceUpdateMode.OnPropertyChanged**, String.Empty)

Once you use this, the data binding method will be listening to OnPropertyChanged event raised by the view model properties. So, two way binding will work automatically.

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...