Overview: I've added a Timepicker control to my UI and set the Time property binding to a DateTime property on the associated ViewModel.
But when I debug the solution the setter doesn't get called for the property SelectedParkDuration
.
In order to debug this further I checked the data context for the UI. The data context is set correctly and the getter of the Timepicker is called.
Question: Does anyone know why the setter isn't getting called when I select a value on the time picker at runtime?
Time Picker control definition:
<TimePicker Grid.Row="2"
Grid.Column="1"
Width="270"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Header="Parking Duration"
Time="{Binding SelectedParkDuration}"
/>
Time picker property - SelectedParkDuration defined in the UI's ViewModel:
private DateTime _selectedParkDuration;
public DateTime SelectedParkDuration
{
get
{
return this._selectedParkDuration;
}
set
{
if (_selectedParkDuration != value)
{
_selectedParkDuration = value;
RaisePropertyChanged("SelectedParkDuration");
}
}
}
The solution is to specify two-way binding as @Ken Tucker suggested. Also the type of the property needed to be of type TimeSpan:
private TimeSpan? _selectedParkDuration;
public TimeSpan? SelectedParkDuration
{
get
{
return this._selectedParkDuration;
}
set
{
if (_selectedParkDuration != value)
{
_selectedParkDuration = value;
RaisePropertyChanged("SelectedParkDuration");
}
}
}
Xaml definition of TimePicker:
<TimePicker Grid.Row="2"
Grid.Column="1"
Width="270"
Height="100"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Header="Parking Duration"
Time="{Binding SelectedParkDuration,
Mode=TwoWay}" />