I've run across a problem while developing my first application that has me a bit stumped. On my viewModel, I have a standard ICommand which is bound to a button on my View. The button is intended to allow a user to Save edits to the record they have selected in a ListView. In the ListView, I have bound "SelectedItem" to the following Property in the viewModel:
Private _SelectedPTOAccrualRecord As PTOAccrualRateItem
Public Property SelectedPTOAccrualRecord() As PTOAccrualRateItem
Get
Return _SelectedPTOAccrualRecord
End Get
Set(ByVal value As PTOAccrualRateItem)
'Change value
_SelectedPTOAccrualRecord = value
'Notify of Property change
NotifyPropertyChanged("SelectedPTOAccrualRecord")
'Call CanExecute for SaveAccrualRecordChanges
Me.SaveAccrualRecordChanges.CanExecute(Nothing)
End Set
End PropertyHere is PTOAccrualRateItem with just the first Property (the solution should be easily applicable to the others):
Public Class PTOAccrualRateItem
Private _PTOAccrualEffectiveDate As Date
Public Property PTOAccrualEffectiveDate() As Date
Get
Return _PTOAccrualEffectiveDate
End Get
Set(ByVal value As Date)
'Check to see if value has been changed
If value <> _PTOAccrualEffectiveDate Then
'Change Value
_PTOAccrualEffectiveDate = value
'Set HasBeenEdited to True
_HasBeenEdited = True
End If
End Set
End PropertyHere is the datepicker on the View for PTOAccrualEffectiveDate:
<DatePicker Text="{Binding SelectedPTOAccrualRecord.PTOAccrualEffectiveDate, StringFormat=d}"
Style="{StaticResource datepickerStandard}" Visibility="{Binding AdminRightsVisibility}"/>So, when I select the PTOAccrualRecord from the View, the Textbox populates as expected. I can alter the field and see that the underlying class in my Model is updating the Property. Here is a simplified version of my CanExecute():
Private Function CanSaveAccrualRecordChanges(ByVal param As Object) As Boolean
'Make a record is selected
If IsNothing(_SelectedPTOAccrualRecord) = True Then
Return False
Else
'Make sure the record has been edited
If _SelectedPTOAccrualRecord.HasBeenEdited = True Then
Return True
Else
Return False
End If
End If
End FunctionSo, if I switch records in the ListView, CanExecute is raised properly and reevaluates whether the button is enabled. However, any changes to the underlying properties of SelectedPTOAccrualRecord do not. I feel like I need to call CanExecute() from my Model, something like this:
Private _PTOAccrualEffectiveDate As Date
Public Property PTOAccrualEffectiveDate() As Date
Get
Return _PTOAccrualEffectiveDate
End Get
Set(ByVal value As Date)
'Check to see if value has been changed
If value <> _PTOAccrualEffectiveDate Then
'Change Value
_PTOAccrualEffectiveDate = value
'Set HasBeenEdited to True
_HasBeenEdited = True
'???? Need to do something here????
viewModelFoo.SaveAccrualRecordChanges.CanExecute()
End If
End Set
End PropertyAm I thinking about this the right way? How do you call CanExecute() from the underlying Model when the Icommand is attached to the ViewModel?
Thanks in advance for the help!!