ListClock.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace HandyControl.Controls;
  7. [TemplatePart(Name = ElementHourList, Type = typeof(ListBox))]
  8. [TemplatePart(Name = ElementMinuteList, Type = typeof(ListBox))]
  9. [TemplatePart(Name = ElementSecondList, Type = typeof(ListBox))]
  10. public class ListClock : ClockBase
  11. {
  12. #region Constants
  13. private const string ElementHourList = "PART_HourList";
  14. private const string ElementMinuteList = "PART_MinuteList";
  15. private const string ElementSecondList = "PART_SecondList";
  16. #endregion Constants
  17. #region Data
  18. private ListBox _hourList;
  19. private ListBox _minuteList;
  20. private ListBox _secondList;
  21. #endregion Data
  22. public override void OnClockOpened() => ScrollIntoView();
  23. public override void OnApplyTemplate()
  24. {
  25. AppliedTemplate = false;
  26. if (ButtonConfirm != null)
  27. {
  28. ButtonConfirm.Click -= ButtonConfirm_OnClick;
  29. }
  30. if (_hourList != null)
  31. {
  32. _hourList.SelectionChanged -= HourList_SelectionChanged;
  33. }
  34. if (_minuteList != null)
  35. {
  36. _minuteList.SelectionChanged -= MinuteList_SelectionChanged;
  37. }
  38. if (_secondList != null)
  39. {
  40. _secondList.SelectionChanged -= SecondList_SelectionChanged;
  41. }
  42. base.OnApplyTemplate();
  43. _hourList = GetTemplateChild(ElementHourList) as ListBox;
  44. if (_hourList != null)
  45. {
  46. CreateItemsSource(_hourList, 24);
  47. _hourList.SelectionChanged += HourList_SelectionChanged;
  48. }
  49. _minuteList = GetTemplateChild(ElementMinuteList) as ListBox;
  50. if (_minuteList != null)
  51. {
  52. CreateItemsSource(_minuteList, 60);
  53. _minuteList.SelectionChanged += MinuteList_SelectionChanged;
  54. }
  55. _secondList = GetTemplateChild(ElementSecondList) as ListBox;
  56. if (_secondList != null)
  57. {
  58. CreateItemsSource(_secondList, 60);
  59. _secondList.SelectionChanged += SecondList_SelectionChanged;
  60. }
  61. ButtonConfirm = GetTemplateChild(ElementButtonConfirm) as Button;
  62. if (ButtonConfirm != null)
  63. {
  64. ButtonConfirm.Click += ButtonConfirm_OnClick;
  65. }
  66. AppliedTemplate = true;
  67. if (SelectedTime.HasValue)
  68. {
  69. Update(SelectedTime.Value);
  70. }
  71. else
  72. {
  73. DisplayTime = DateTime.Now;
  74. Update(DisplayTime);
  75. }
  76. }
  77. /// <summary>
  78. /// 更新
  79. /// </summary>
  80. /// <param name="time"></param>
  81. internal override void Update(DateTime time)
  82. {
  83. if (!AppliedTemplate) return;
  84. var h = time.Hour;
  85. var m = time.Minute;
  86. var s = time.Second;
  87. _hourList.SelectedIndex = h;
  88. _minuteList.SelectedIndex = m;
  89. _secondList.SelectedIndex = s;
  90. ScrollIntoView();
  91. DisplayTime = time;
  92. }
  93. private void HourList_SelectionChanged(object sender, SelectionChangedEventArgs e) => Update();
  94. private void MinuteList_SelectionChanged(object sender, SelectionChangedEventArgs e) => Update();
  95. private void SecondList_SelectionChanged(object sender, SelectionChangedEventArgs e) => Update();
  96. private void CreateItemsSource(ItemsControl selector, int count)
  97. {
  98. var list = new List<string>();
  99. for (var i = 0; i < count; i++)
  100. {
  101. list.Add(i.ToString("#00"));
  102. }
  103. selector.ItemsSource = list;
  104. }
  105. [SuppressMessage("ReSharper", "MergeIntoPattern")]
  106. private void Update()
  107. {
  108. if (_hourList.SelectedIndex >= 0 && _hourList.SelectedIndex < 24 &&
  109. _minuteList.SelectedIndex >= 0 && _minuteList.SelectedIndex < 60 &&
  110. _secondList.SelectedIndex >= 0 && _secondList.SelectedIndex < 60)
  111. {
  112. var now = DateTime.Now;
  113. DisplayTime = new DateTime(now.Year, now.Month, now.Day, _hourList.SelectedIndex,
  114. _minuteList.SelectedIndex, _secondList.SelectedIndex);
  115. }
  116. }
  117. private void ScrollIntoView()
  118. {
  119. _hourList.ScrollIntoView(_hourList.SelectedItem);
  120. _minuteList.ScrollIntoView(_minuteList.SelectedItem);
  121. _secondList.ScrollIntoView(_secondList.SelectedItem);
  122. }
  123. }