/*************************************************************************************
Toolkit for WPF
Copyright (C) 2007-2022 Xceed Software Inc.
This program is provided to you under the terms of the XCEED SOFTWARE, INC.
COMMUNITY LICENSE AGREEMENT (for non-commercial use) as published at
https://github.com/xceedsoftware/wpftoolkit/blob/master/license.md
For more features, controls, and fast professional support,
pick up the Plus Edition at https://xceed.com/xceed-toolkit-plus-for-wpf/
Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
***********************************************************************************/
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.Primitives;
using System.Diagnostics;
using System.Windows.Media;
#if VS2008
using Microsoft.Windows.Controls;
using Microsoft.Windows.Controls.Primitives;
#endif
namespace Xceed.Wpf.Toolkit
{
[TemplatePart( Name = PART_Calendar, Type = typeof( Calendar ) )]
[TemplatePart( Name = PART_TimeUpDown, Type = typeof( TimePicker ) )]
public class DateTimePicker : DateTimePickerBase
{
private const string PART_Calendar = "PART_Calendar";
private const string PART_TimeUpDown = "PART_TimeUpDown";
#region Members
private Calendar _calendar;
private TimePicker _timePicker;
private DateTime? _calendarTemporaryDateTime;
private DateTime? _calendarIntendedDateTime;
private bool _isModifyingCalendarInternally;
#endregion //Members
#region Properties
#region AutoCloseCalendar
public static readonly DependencyProperty AutoCloseCalendarProperty = DependencyProperty.Register( "AutoCloseCalendar", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( false ) );
public bool AutoCloseCalendar
{
get
{
return ( bool )GetValue( AutoCloseCalendarProperty );
}
set
{
SetValue( AutoCloseCalendarProperty, value );
}
}
#endregion //AutoCloseCalendar
#region AutoCloseCalendarOnTimeSelection
public static readonly DependencyProperty AutoCloseCalendarOnTimeSelectionProperty = DependencyProperty.Register( "AutoCloseCalendarOnTimeSelection", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( false ) );
public bool AutoCloseCalendarOnTimeSelection
{
get
{
return ( bool )GetValue( AutoCloseCalendarOnTimeSelectionProperty );
}
set
{
SetValue( AutoCloseCalendarOnTimeSelectionProperty, value );
}
}
#endregion //AutoCloseCalendarOnTimeSelection
#region CalendarDisplayMode
public static readonly DependencyProperty CalendarDisplayModeProperty = DependencyProperty.Register( "CalendarDisplayMode", typeof( CalendarMode )
, typeof( DateTimePicker ), new UIPropertyMetadata( CalendarMode.Month ) );
public CalendarMode CalendarDisplayMode
{
get
{
return ( CalendarMode )GetValue( CalendarDisplayModeProperty );
}
set
{
SetValue( CalendarDisplayModeProperty, value );
}
}
#endregion //CalendarDisplayMode
#region CalendarWidth
public static readonly DependencyProperty CalendarWidthProperty = DependencyProperty.Register( "CalendarWidth", typeof( double )
, typeof( DateTimePicker ), new UIPropertyMetadata( 178d ) );
public double CalendarWidth
{
get
{
return ( double )GetValue( CalendarWidthProperty );
}
set
{
SetValue( CalendarWidthProperty, value );
}
}
#endregion //CalendarWidth
#region TimeFormat
public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register( "TimeFormat", typeof( DateTimeFormat ), typeof( DateTimePicker ), new UIPropertyMetadata( DateTimeFormat.ShortTime ) );
public DateTimeFormat TimeFormat
{
get
{
return ( DateTimeFormat )GetValue( TimeFormatProperty );
}
set
{
SetValue( TimeFormatProperty, value );
}
}
#endregion //TimeFormat
#region TimeFormatString
public static readonly DependencyProperty TimeFormatStringProperty = DependencyProperty.Register( "TimeFormatString", typeof( string ), typeof( DateTimePicker ), new UIPropertyMetadata( default( String ) ), IsTimeFormatStringValid );
public string TimeFormatString
{
get
{
return ( string )GetValue( TimeFormatStringProperty );
}
set
{
SetValue( TimeFormatStringProperty, value );
}
}
private static bool IsTimeFormatStringValid( object value )
{
return DateTimeUpDown.IsFormatStringValid( value );
}
#endregion //TimeFormatString
#region TimePickerAllowSpin
public static readonly DependencyProperty TimePickerAllowSpinProperty = DependencyProperty.Register( "TimePickerAllowSpin", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( true ) );
public bool TimePickerAllowSpin
{
get
{
return ( bool )GetValue( TimePickerAllowSpinProperty );
}
set
{
SetValue( TimePickerAllowSpinProperty, value );
}
}
#endregion //TimePickerAllowSpin
#region TimePickerTimeListItemsStyle
///
/// TimePickerTimeListItemsStyle Dependency Property
///
public static readonly DependencyProperty TimePickerTimeListItemsStyleProperty = DependencyProperty.Register( "TimePickerTimeListItemsStyle", typeof( Style ), typeof( DateTimePicker ),
new FrameworkPropertyMetadata( ( Style )null, new PropertyChangedCallback( OnTimePickerTimeListItemsStyleChanged ) ) );
///
/// Gets or sets the TimePickerTimeListItemsStyle property.
/// This dependency property indicates the style to apply to TimeListItems objects.
///
public Style TimePickerTimeListItemsStyle
{
get
{
return ( Style )GetValue( TimePickerTimeListItemsStyleProperty );
}
set
{
SetValue( TimePickerTimeListItemsStyleProperty, value );
}
}
///
/// Handles changes to the TimePickerTimeListItemsStyle property.
///
private static void OnTimePickerTimeListItemsStyleChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
{
( ( DateTimePicker )d ).OnTimePickerTimeListItemsStyleChanged( e );
}
///
/// Provides derived classes an opportunity to handle changes to the TimePickerTimeListItemsStyle property.
///
protected virtual void OnTimePickerTimeListItemsStyleChanged( DependencyPropertyChangedEventArgs e )
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion
#region TimePickerShowButtonSpinner
public static readonly DependencyProperty TimePickerShowButtonSpinnerProperty = DependencyProperty.Register( "TimePickerShowButtonSpinner", typeof( bool ), typeof( DateTimePicker ), new UIPropertyMetadata( true ) );
public bool TimePickerShowButtonSpinner
{
get
{
return ( bool )GetValue( TimePickerShowButtonSpinnerProperty );
}
set
{
SetValue( TimePickerShowButtonSpinnerProperty, value );
}
}
#endregion //TimePickerShowButtonSpinner
#region TimePickerVisibility
public static readonly DependencyProperty TimePickerVisibilityProperty = DependencyProperty.Register( "TimePickerVisibility", typeof( Visibility ), typeof( DateTimePicker ), new UIPropertyMetadata( Visibility.Visible ) );
public Visibility TimePickerVisibility
{
get
{
return ( Visibility )GetValue( TimePickerVisibilityProperty );
}
set
{
SetValue( TimePickerVisibilityProperty, value );
}
}
#endregion //TimePickerVisibility
#region TimeWatermark
public static readonly DependencyProperty TimeWatermarkProperty = DependencyProperty.Register( "TimeWatermark", typeof( object ), typeof( DateTimePicker ), new UIPropertyMetadata( null ) );
public object TimeWatermark
{
get
{
return ( object )GetValue( TimeWatermarkProperty );
}
set
{
SetValue( TimeWatermarkProperty, value );
}
}
#endregion //TimeWatermark
#region TimeWatermarkTemplate
public static readonly DependencyProperty TimeWatermarkTemplateProperty = DependencyProperty.Register( "TimeWatermarkTemplate", typeof( DataTemplate ), typeof( DateTimePicker ), new UIPropertyMetadata( null ) );
public DataTemplate TimeWatermarkTemplate
{
get
{
return ( DataTemplate )GetValue( TimeWatermarkTemplateProperty );
}
set
{
SetValue( TimeWatermarkTemplateProperty, value );
}
}
#endregion //TimeWatermarkTemplate
#endregion //Properties
#region Constructors
static DateTimePicker()
{
DefaultStyleKeyProperty.OverrideMetadata( typeof( DateTimePicker ), new FrameworkPropertyMetadata( typeof( DateTimePicker ) ) );
UpdateValueOnEnterKeyProperty.OverrideMetadata( typeof( DateTimePicker ), new FrameworkPropertyMetadata( true ) );
}
public DateTimePicker()
{
EventManager.RegisterClassHandler( typeof( ListBoxItem ), PreviewMouseUpEvent, new RoutedEventHandler( PreviewMouseUpEventHandler ) );
}
#endregion //Constructors
#region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if( _calendar != null )
{
_calendar.SelectedDatesChanged -= this.Calendar_SelectedDatesChanged;
_calendar.MouseDoubleClick -= this.Calendar_MouseDoubleClick;
}
_calendar = GetTemplateChild( PART_Calendar ) as Calendar;
if( _calendar != null )
{
_calendar.Language = XmlLanguage.GetLanguage( System.Globalization.CultureInfo.CurrentCulture.IetfLanguageTag );
_calendar.SelectedDatesChanged += this.Calendar_SelectedDatesChanged;
_calendar.MouseDoubleClick += this.Calendar_MouseDoubleClick;
_calendar.SelectedDate = Value ?? null;
_calendar.DisplayDate = Value ?? this.ContextNow;
this.SetBlackOutDates();
}
if( _timePicker != null )
{
_timePicker.ValueChanged -= this.TimePicker_ValueChanged;
}
_timePicker = GetTemplateChild( PART_TimeUpDown ) as TimePicker;
if( _timePicker != null )
{
_timePicker.ValueChanged += this.TimePicker_ValueChanged;
}
}
protected override void OnPreviewMouseUp( MouseButtonEventArgs e )
{
if( Mouse.Captured is CalendarItem )
{
Mouse.Capture( null );
// Do not close calendar on Year/Month Selection. Close only on Day selection.
if( AutoCloseCalendar && ( _calendar != null ) && ( _calendar.DisplayMode == CalendarMode.Month ) )
{
ClosePopup( true );
}
}
base.OnPreviewMouseUp( e );
}
protected override void OnValueChanged( DateTime? oldValue, DateTime? newValue )
{
//The calendar only select the Date part, not the time part.
DateTime? newValueDate = ( newValue != null )
? newValue.Value.Date
: ( DateTime? )null;
if( _calendar != null && _calendar.SelectedDate != newValueDate )
{
_isModifyingCalendarInternally = true;
_calendar.SelectedDate = newValueDate;
_calendar.DisplayDate = newValue.GetValueOrDefault( this.ContextNow );
_isModifyingCalendarInternally = false;
}
//If we change any part of the datetime without
//using the calendar when the actual date is temporary,
//clear the temporary value.
if( ( _calendar != null ) && ( _calendarTemporaryDateTime != null ) && ( newValue != _calendarTemporaryDateTime ) )
{
_calendarTemporaryDateTime = null;
_calendarIntendedDateTime = null;
}
if( _timePicker != null )
{
// sync TimePicker.TempValue with current DatetimePicker.Value
_timePicker.UpdateTempValue( newValue );
}
base.OnValueChanged( oldValue, newValue );
}
protected override void OnIsOpenChanged( bool oldValue, bool newValue )
{
base.OnIsOpenChanged( oldValue, newValue );
if( !newValue )
{
_calendarTemporaryDateTime = null;
_calendarIntendedDateTime = null;
}
}
protected override void OnPreviewKeyDown( KeyEventArgs e )
{
//if the calendar is open then we don't want to modify the behavior of navigating the calendar control with the Up/Down keys.
if( !IsOpen )
base.OnPreviewKeyDown( e );
}
protected override void OnMaximumChanged( DateTime? oldValue, DateTime? newValue )
{
base.OnMaximumChanged( oldValue, newValue );
this.SetBlackOutDates();
}
protected override void OnMinimumChanged( DateTime? oldValue, DateTime? newValue )
{
base.OnMinimumChanged( oldValue, newValue );
this.SetBlackOutDates();
}
#endregion //Base Class Overrides
#region Command Hanlders
#endregion // Command Handlers
#region Events
#region Today Event
public static readonly RoutedEvent TodayEvent = EventManager.RegisterRoutedEvent( "Today", RoutingStrategy.Bubble, typeof( EventHandler ), typeof( DateTimePicker ) );
public event RoutedEventHandler Today
{
add
{
AddHandler( TodayEvent, value );
}
remove
{
RemoveHandler( TodayEvent, value );
}
}
#endregion //Today Event
#endregion // Events
#region Event Handlers
protected override void HandleKeyDown( object sender, KeyEventArgs e )
{
// The base call will handle the Ctrl+Down, Enter and Esc keys
// in order to open or close the popup.
// Do not close the Calendar if the call is handled
// by the TimePicker inside the DateTimePicker template.
if( IsOpen
&& ( _timePicker != null )
&& _timePicker.IsKeyboardFocusWithin
&& ( _timePicker.IsOpen || e.Handled ) )
return;
base.HandleKeyDown( sender, e );
}
private void TimePicker_ValueChanged( object sender, RoutedPropertyChangedEventArgs