PropertyChanging - how to reject the change

Dec 6, 2010 at 2:24 PM

I see in the new version that the objects create a generic PropertyChanging event.  The PropertyChangingEventArgs doesn't seem to have a CANCEL property however.  The individual xxxChanging events that can be enabled in the model all have a CANCEL property.  Was this an oversight and do you intend to add a similar CANCEL property?  Or do you recommend throwing an exception to interrupt the flow?

Dec 7, 2010 at 12:34 PM

The default PropertyChanging event is part of the .NET framework in the INotifyPropertyChanged and INotifyPropertyChanging interfaces. These have no cancel. We have also implemented a set of events that are type specific for each property changing and it is cancel-able. You must enable the model property EnableCustomChangeEvents to get the customized event for each property. This will generate an "[PropertyName]Changing" and "[PropertyName]Changed" event that will do what you want.

Dec 7, 2010 at 5:34 PM

It is in a WinForm application not a Web application.  Should I be using something other than DataGridView?

What seems most confusing for me is that when using a collection in general it requires putting it into an ILIST and then traversing each item doing an AddHandler for Validate, PropertyChanging and PropertyChanged.  In order to use the custom xxxChanging events which are cancelable you would have to AddHandler each of those as well.  So if there are 1000 items and 20 fields that you want to monitor you would have to AddHandler 1000x20x2 times.

What might be preferable is for the collection to fire off its own Validate, PropertyChanging (cancelable) and PropertyChanged events and having it include the instance item in the parameter list.  Then we only ever need 3 event handlers for any collection regardless of the number of items.

Sample of what I am thinking:

Private WithEvents myCollection as xxx.Business.Objects.UserCollection  

Public Sub myCollection_PropertyChanging( item as xxx.Business.Objects.User, _
                  PropName as string, _
                 e as eventPropertyChangingArgs) _
                 Handles myCollection.PropertyChanging

      Select Case PropName
          Case “UserId”
             e.Cancel = True
          case “UserName”
             If item.UserName.Contains(“!”) Then
                e.cancel = True
             End If
      End Select

End Sub  


Public Sub myCollection_PropertyChanged( item as xxx.Business.Objects.User, _
                  PropName as string, _
                  e as eventPropertyChangedArgs) _
         Handles myCollection.PropertyChanged

       Select Case PropName
          case “UserPriv1”
             Call ManagerLog(item.UserId & “ changed his login privilege”)
      End Select

End Sub