PropertyChanged - RemoveHandler - bug???PropertyChanged - RemoveHandler - bug???
Old forum URL: forums.lhotka.net/forums/t/3653.aspx
RandyH posted on Friday, October 05, 2007
I have class (MyEmployee) that inherits from businessbase. It has one property (Name). An instance of MyEmployee (_emp) is the datasource for a BindingSource (EmployeeBindingSource). EmployeeBindingSource is bound (Name) to a textbox.text property. In code, the first time I try to update _emp.Name the textbox is updated. The second time I try to update _emp.Name the textbox is not updated.
When I step through the first time , I noticed that in BindableBase.RaiseEvent mNonSerializableHandlers.Invoke(sender, e) was being called. This then triggered RemoveHandler to be called so that the PropertyChanged event was REMOVED. Subsequently, the second time I updated _emp.Name the PropertyChanged event never notified the BindingSource.
Any help here greatly appreciated! I have include the very simple code below (both employee and form):
Public Class Form2
Private _emp As MyEmployee
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_emp = New MyEmployee
_emp.Name = "Howdy Dudey"
EmployeeBindingSource.DataSource = _emp
Me.TextBox1.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.EmployeeBindingSource, "Name", True))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
_emp.Name = "John Doe"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
_emp.Name = "Bubba Smith"
End Sub
End Class
<Serializable()> _
Public Class MyEmployee
Inherits Csla.BusinessBase(Of Employee)
Private _Name As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
PropertyHasChanged()
End Set
End Property
Protected Overrides Function GetIdValue() As Object
Return Guid.NewGuid
End Function
End Class
RandyH replied on Monday, October 08, 2007
Also, if MyEmployee inherits from BindableBase everything works great. But, if I switch it back to BusinessBase the propertychange event only gets fired once because the RemoveHandler gets called.
RockfordLhotka replied on Monday, October 08, 2007
Have you tried using the drag-and-drop designer in VS to set up the data binding, and then comparing your manual code to what the designer creates?
I have high confidence that the binding support in CSLA works, I'm working on a couple apps that use it, and they work fine. But I always use the designer to do the binding, so I'm not entirely sure what the low-level code should look like.
My guess is that your data binding code in the UI is incorrect, but the designer will do it right, and so if you compare results perhaps you'll discover the issue in the UI code.
Copyright (c) Marimer LLC