private void Calendar_SelectedDatesChanged( object sender, SelectionChangedEventArgs e )
{
if( e.AddedItems.Count > 0 )
{
var newDate = ( DateTime? )e.AddedItems[ 0 ];
if( newDate != null )
{
//The Calendar will always return a date with an "Unspecified" Kind.
//Force the expected kind to the value.
newDate = DateTime.SpecifyKind( newDate.Value, this.Kind );
// Only change the year, month, and day part of the value. Keep everything to the last "tick."
// "Milliseconds" aren't precise enough. Use a mathematical scheme instead.
if( _calendarIntendedDateTime != null )
{
newDate = newDate.Value.Date + _calendarIntendedDateTime.Value.TimeOfDay;
_calendarTemporaryDateTime = null;
_calendarIntendedDateTime = null;
}
else if( Value != null )
{
newDate = newDate.Value.Date + Value.Value.TimeOfDay;
}
// Always be sure that the time part of the selected value is always
// within the bound of the min max. The time part could be altered
// if the calendar's selected date match the Minimum or Maximum date.
// Keep in memory the intended time of day, in case that the selected
// calendar date is only transitory (browsing the calendar with the keyboard)
var limitedDateTime = this.GetClippedMinMaxValue( newDate );
if( limitedDateTime.Value != newDate.Value )
{
_calendarTemporaryDateTime = limitedDateTime;
_calendarIntendedDateTime = newDate;
newDate = limitedDateTime;
}
}
if( !object.Equals( newDate, Value ) )
Value = newDate;
}
}