Prevent property change in databinding and force bound control to show current value
-
21-08-2019 - |
Question
I have
Public Overrides Property UID() As String
Get
Return mUID
End Get
Set(ByVal value As String)
If Me.IsNew Then
mUID = value
End If
OnPropertyChanged("UID")
End Set
End Property
The class implements INotifyPropertyChanged and I am binding to a TextBox control in WinForm controls.
The problem is when the user types a value into the textbox that new value is shown in the textbox even though the bound value remains the same do to the fact IsNew is false. (I have tried using both types DataSourceUpdateMode.)
I have read INotifyPropertyChanged problem and I believe windows forms works basically the same way in that the initiator of the event will not also detect handle the event.
Is there anything I can do in the BO classes to force the control to responded to the INotifyPropertyChanged event? Or is there something else I can to within the BO class to get the control to display the correct value?
I realize I could handle this in the GUI code in various ways but that is not my goal.
Solution
I found an answer.
First this is a problem and has been noted by Rocky Lhotka. The solution I used came from him.
First he has a control you can drop on the form or user control and it solves the problem.
He also had some code that I slightly modified and used and placed it on our base form and user control.
Private Sub baseUserControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
HookBindings(Me)
End Sub
Private Sub HookBindings(ByVal CNTRL As Control)
For Each ctl As Control In CNTRL.Controls
If Not TypeOf ctl Is baseUserControl Then
For Each b As Binding In ctl.DataBindings
AddHandler b.BindingComplete, AddressOf ReadBinding
Next
HookBindings(ctl)
End If
Next
End Sub
Private Sub ReadBinding(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)
e.Binding.ReadValue()
End Sub