using System; using System.Drawing; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace DTPicker { /// Why I made this fork => /// 1. I wanted to create a simple control with no external dependencies (which means not using WPFToolkit) /// 2. I wanted to add time selection controls instead of using the adorners /// 3. The textbox also allowed users to type whatever they wanted and then there were checks put in place /// to track and correct them or to ask users to make corrections. Too complex in my view for a simple task /// /// Changes Made => /// 1. Uses the WPF calendar control instead of the WPFToolkit calendar /// 2. Uses dropdowns for selecting time and has a save button to update the textbox /// /// Things that will be needed for this control to work properly (and look good :) ) => /// 1. A bitmap image 32x32 added as an embedded resource /// /// Licensing => /// The Code Project Open License (CPOL) /// http://www.codeproject.com/info/cpol10.aspx public partial class DateTimePicker : UserControl { public DateTimePicker() { InitializeComponent(); CalDisplay.SelectedDatesChanged += CalDisplay_SelectedDatesChanged; BitmapSource ConvertGDI_To_WPF(Bitmap bm) { BitmapSource bms = null; IntPtr h_bm = IntPtr.Zero; h_bm = bm.GetHbitmap(); bms = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(h_bm, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bms.Freeze(); h_bm = IntPtr.Zero; return bms; } Bitmap bitmap1 = Properties.Resources.datetime; bitmap1.MakeTransparent(Color.Black); CalIco.Source = ConvertGDI_To_WPF(bitmap1); } #region "EventHandlers" private void CalDisplay_SelectedDatesChanged(object sender, EventArgs e) { PopUpCalendarButton.IsChecked = true; if (Hours.SelectedIndex == -1 || Min.SelectedIndex == -1 || AMPM.SelectedIndex == -1) { var date = CalDisplay.SelectedDate.Value.Date + DateTime.Now.TimeOfDay; DateDisplay.Text = date.ToString("dd-MM-yyyy hh:mm tt"); return; } if (AMPM.Text == "PM") { int hours = Convert.ToInt32(Hours.Text)+12; TimeSpan timeSpan = TimeSpan.Parse(hours.ToString() + ":" + Min.Text); if (CalDisplay.SelectedDate.Value.Date == DateTime.Today.Date && timeSpan.CompareTo(DateTime.Now.TimeOfDay)<0) { var date = CalDisplay.SelectedDate.Value.Date + DateTime.Now.TimeOfDay; DateDisplay.Text = date.ToString("dd-MM-yyyy hh:mm tt"); } else { var date = CalDisplay.SelectedDate.Value.Date + timeSpan; DateDisplay.Text = date.ToString("dd-MM-yyyy hh:mm tt"); } } else { TimeSpan timeSpan = TimeSpan.Parse(Hours.Text + ":" + Min.Text); if (CalDisplay.SelectedDate.Value.Date == DateTime.Today.Date && timeSpan.CompareTo(DateTime.Now.TimeOfDay) < 0) { var date = CalDisplay.SelectedDate.Value.Date + DateTime.Now.TimeOfDay; DateDisplay.Text = date.ToString("dd-MM-yyyy hh:mm tt"); } else { var date = CalDisplay.SelectedDate.Value.Date + timeSpan; DateDisplay.Text = date.ToString("dd-MM-yyyy hh:mm tt"); } } } private void SaveTime_Click(object sender, RoutedEventArgs e) { if (CalDisplay.SelectedDate.Value.Date == null) { CalDisplay.SelectedDate = DateTime.Today.Date; CalDisplay.DisplayDate = DateTime.Today.Date; } if (Hours.SelectedIndex == -1 || Min.SelectedIndex == -1 || AMPM.SelectedIndex == -1) {} else { CalDisplay_SelectedDatesChanged(SaveTime, EventArgs.Empty); } PopUpCalendarButton.IsChecked = false; } #endregion } }