MonthCalendar.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. namespace System.Windows.Forms
  2. {
  3. public class MonthCalendar : Control
  4. {
  5. protected new CustomControls.Calendar control { get; }
  6. public DateTime MinDate
  7. {
  8. get => control.DisplayDateStart ?? DateTime.MinValue;
  9. set => control.DisplayDateStart = value;
  10. }
  11. public DateTime MaxDate
  12. {
  13. get => control.DisplayDateEnd ?? DateTime.MaxValue;
  14. set => control.DisplayDateEnd = value;
  15. }
  16. private DateTime selectionStart = DateTime.MinValue;
  17. public DateTime SelectionStart
  18. {
  19. get => selectionStart;
  20. set
  21. {
  22. selectionStart = value;
  23. UpdateSelectionRange();
  24. }
  25. }
  26. private DateTime selectionEnd;
  27. public DateTime SelectionEnd
  28. {
  29. get => selectionEnd;
  30. set
  31. {
  32. selectionEnd = value;
  33. UpdateSelectionRange();
  34. }
  35. }
  36. public SelectionRange SelectionRange
  37. {
  38. get => GetSelectedRange();
  39. set
  40. {
  41. SelectionStart = value.Start;
  42. SelectionEnd = value.End;
  43. }
  44. }
  45. public int MaxSelectionCount { get; set; } // TODO?
  46. public Drawing.Size CalendarDimensions { get; set; } // TODO?
  47. public Day FirstDayOfWeek
  48. {
  49. get
  50. {
  51. return control.ReadLocalValue(System.Windows.Controls.Calendar.FirstDayOfWeekProperty) == DependencyProperty.UnsetValue ?
  52. Day.Default : Helper.GetDay(control.FirstDayOfWeek);
  53. }
  54. set
  55. {
  56. if (value == Day.Default)
  57. control.ClearValue(System.Windows.Controls.Calendar.FirstDayOfWeekProperty);
  58. else
  59. control.FirstDayOfWeek = Helper.GetDayOfWeek(value);
  60. }
  61. }
  62. public bool ShowToday
  63. {
  64. get => control.IsTodayHighlighted;
  65. set => control.IsTodayHighlighted = value;
  66. }
  67. public bool ShowTodayCircle { get; set; } // nope
  68. public bool ShowWeekNumbers { get; set; } // nope
  69. public DateTime TodayDate { get; set; } // nope
  70. public DateTime[] AnnuallyBoldedDates
  71. {
  72. get => control.AnnualDates;
  73. set => control.AnnualDates = value;
  74. }
  75. public DateTime[] MonthlyBoldedDates
  76. {
  77. get => control.MonthDates;
  78. set => control.MonthDates = value;
  79. }
  80. public DateTime[] BoldedDates
  81. {
  82. get => control.Dates;
  83. set => control.Dates = value;
  84. }
  85. public event DateRangeEventHandler DateChanged;
  86. public event DateRangeEventHandler DateSelected;
  87. private void UpdateSelectionRange()
  88. {
  89. control.SelectedDates.Clear();
  90. var start = SelectionStart;
  91. var end = SelectionEnd;
  92. if (start >= MinDate && start <= MaxDate &&
  93. end >= MinDate && end <= MaxDate &&
  94. start <= end)
  95. {
  96. control.SelectedDates.AddRange(SelectionStart, SelectionEnd);
  97. }
  98. }
  99. private SelectionRange GetSelectedRange()
  100. {
  101. var start = DateTime.MaxValue;
  102. var end = DateTime.MinValue;
  103. foreach (var date in control.SelectedDates)
  104. {
  105. if (date > end)
  106. end = date;
  107. if (date < start)
  108. start = date;
  109. }
  110. return new SelectionRange(start, end.Add(new TimeSpan(23, 59, 59)));
  111. }
  112. protected virtual void OnDateSelected(DateRangeEventArgs e)
  113. {
  114. DateChanged?.Invoke(this, e);
  115. DateSelected?.Invoke(this, e);
  116. }
  117. public MonthCalendar()
  118. {
  119. control = new();
  120. SetControl(control);
  121. AutoSize = true;
  122. control.SelectionMode = Windows.Controls.CalendarSelectionMode.SingleRange;
  123. control.SelectedDatesChanged += (s, e) =>
  124. {
  125. var range = GetSelectedRange();
  126. selectionStart = range.Start;
  127. selectionEnd = range.End;
  128. OnDateSelected(new DateRangeEventArgs(selectionStart, selectionEnd));
  129. };
  130. }
  131. }
  132. }