I have an observable collection binded to a datagrid. On a datagrid row, when I edit a property’s value I need another property on that row to update it’s value based on a calculation. What is the best way to go about doing this. I tried creating a collectionchanged method like below:
Public WithEvents Tickets As ObservableCollection(Of Ticket)
Public Sub Tickets_CollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs) Handles TicketCollection.CollectionChanged
CalculatedVariable = determineCalculation()
End Sub
However this it only called when a new item is added or deleted, not when a property within the row is edited. Another way I looked at is calling the calculation method within the setter of each property but that seems messy knowing that i would need to call that method and update the property on several different properties that I need it on. Is there a good easy efficient way that I can accomplish what I’m trying to do? Thannks for any feedback 🙂
public property MyVariable
Get
return _MyVariable
Set(value)
_MyVariable = value
CalculatedVariable = determineCalculation()
RaisePropertyChange("MyVariable") , ect..
EndProperty
The class of the elements listed by your ObservableCollection has to implement INotifyPropertyChanged. You have to raise the PropertyChanged event in your setters. Then you can subscribe to this event inside your class and make the calculation in the callback. I find it a little bit cleaner than doing the calculation in each setter, and anyways you need to implement INotifyPropertyChanged if you want the UI to be updated when you change any property of your object.