TimePicker.xaml.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace NEIntelligentControl2.Views.Basic
  17. {
  18. /// <summary>
  19. /// 时间选择控件
  20. /// </summary>
  21. public partial class TimePicker : UserControl
  22. {
  23. public static readonly DependencyProperty SelectedDateTimeProperty = DependencyProperty.Register("SelectedDateTime", typeof(DateTime), typeof(TimePicker));
  24. public static readonly RoutedEvent SelectedDatesChangedEvent = EventManager.RegisterRoutedEvent("SelectedTimeChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TimePicker));
  25. /// <summary>
  26. /// 选中的时间
  27. /// </summary>
  28. public DateTime SelectedDateTime { get => (DateTime)GetValue(SelectedDateTimeProperty); set => SetValue(SelectedDateTimeProperty, value); }
  29. /// <summary>
  30. /// 时间选择改变事件
  31. /// </summary>
  32. public event RoutedEventHandler SelectedDateTimeChanged
  33. {
  34. add
  35. {
  36. AddHandler(SelectedDatesChangedEvent, value);
  37. }
  38. remove
  39. {
  40. RemoveHandler(SelectedDatesChangedEvent, value);
  41. }
  42. }
  43. private DateTime _StartTime;
  44. public TimePicker()
  45. {
  46. InitializeComponent();
  47. var dt = DateTime.Now;
  48. SelectedDateTime = dt.AddSeconds(-dt.Second);
  49. _StartTime = SelectedDateTime;
  50. Init();
  51. }
  52. private void Init()
  53. {
  54. int[] h = new int[24];
  55. int[] m = new int[60];
  56. for (int i = 0; i < 60; ++i)
  57. {
  58. if (i < 24) h[i] = i;
  59. m[i] = i;
  60. }
  61. _lbhour.ItemsSource = h;
  62. _lbminute.ItemsSource = m;
  63. _lbhour.SelectedIndex = SelectedDateTime.Hour;
  64. _lbminute.SelectedIndex = SelectedDateTime.Minute;
  65. }
  66. private void TextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  67. {
  68. _popup.IsOpen = !_popup.IsOpen;
  69. if (SelectedDateTime.CompareTo(_StartTime) == 0 || _popup.IsOpen) return;
  70. }
  71. private void Button_Click(object sender, RoutedEventArgs e)
  72. {
  73. _popup.IsOpen = false;
  74. switch (((Control)sender).Tag)
  75. {
  76. case "cancel":
  77. SelectedDateTime = _StartTime;
  78. break;
  79. case "ok":
  80. _StartTime = default(DateTime);
  81. break;
  82. default: return;
  83. }
  84. }
  85. private void DateTimeChanged()
  86. {
  87. _StartTime = SelectedDateTime;
  88. RoutedEventArgs rea = new RoutedEventArgs(SelectedDatesChangedEvent, this);
  89. this.RaiseEvent(rea);
  90. }
  91. private async void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
  92. {
  93. var date = (DateTime)e.AddedItems[0];
  94. SelectedDateTime = date.Add(SelectedDateTime.TimeOfDay);
  95. Mouse.Capture(null);
  96. await Task.Delay(100);
  97. _tb.Focus();
  98. }
  99. private async void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  100. {
  101. int i = (int)e.AddedItems[0];
  102. if (((Control)sender).Tag as string == "minute")
  103. {
  104. i -= SelectedDateTime.Minute;
  105. SelectedDateTime = SelectedDateTime.AddMinutes(i);
  106. }
  107. else
  108. {
  109. i -= SelectedDateTime.Hour;
  110. SelectedDateTime = SelectedDateTime.AddHours(i);
  111. }
  112. await Task.Delay(200);
  113. _tb.Focus();
  114. }
  115. private async void TextBox_LostFocus(object sender, RoutedEventArgs e)
  116. {
  117. await Task.Delay(200);
  118. if (_tb.IsFocused && _popup.IsMouseOver) return;
  119. _popup.IsOpen = false;
  120. if (SelectedDateTime.CompareTo(_StartTime) == 0) return;
  121. DateTimeChanged();
  122. }
  123. }
  124. /// <summary>
  125. /// 时间转换
  126. /// </summary>
  127. public class DateTimeConver : IValueConverter
  128. {
  129. //当值从绑定源传播给绑定目标时,调用方法Convert
  130. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  131. {
  132. if (value == null)
  133. return DependencyProperty.UnsetValue;
  134. DateTime date = (DateTime)value;
  135. return date.ToString("yyyy-MM-dd HH:mm:ss");
  136. }
  137. //当值从绑定目标传播给绑定源时,调用此方法ConvertBack
  138. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  139. {
  140. string str = value as string;
  141. DateTime txtDate;
  142. if (DateTime.TryParse(str, out txtDate))
  143. {
  144. return txtDate;
  145. }
  146. return DependencyProperty.UnsetValue;
  147. }
  148. }
  149. }