Dreamine.UI.Wpf.Controls 1.0.1
Dreamine.UI.Wpf.Controls 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineTimeSpinner.cs
이 파일의 문서화 페이지로 가기
1using System.Windows;
2using System.Windows.Controls;
3using System.Windows.Controls.Primitives;
4using System.Windows.Input;
5
7{
16 public class DreamineTimeSpinner : Control
17 {
26 private const string PART_HourBox = "PART_HourBox";
27
36 private const string PART_MinBox = "PART_MinBox";
37
46 private const string PART_SecBox = "PART_SecBox";
47
56 private const string PART_UpButton = "PART_UpButton";
57
66 private const string PART_DownButton = "PART_DownButton";
67
76 private TextBox? _hourBox;
77
86 private TextBox? _minBox;
87
96 private TextBox? _secBox;
97
106 private ButtonBase? _upButton;
107
116 private ButtonBase? _downButton;
117
126 private enum eFocusedBox
127 {
137
147
157 }
158
168
177 public TimeSpan Time
178 {
179 get => (TimeSpan)GetValue(TimeProperty);
180 set => SetValue(TimeProperty, value);
181 }
182
191 public static readonly DependencyProperty TimeProperty =
192 DependencyProperty.Register(
193 nameof(Time),
194 typeof(TimeSpan),
195 typeof(DreamineTimeSpinner),
196 new FrameworkPropertyMetadata(default(TimeSpan), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTimeChanged));
197
222 private static void OnTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
223 {
224 var spinner = d as DreamineTimeSpinner;
225 spinner?.UpdateTextBoxes();
226 }
227
236 public static RoutedCommand UpCommand { get; set; } = new(nameof(UpCommand), typeof(DreamineTimeSpinner));
237
246 public static RoutedCommand DownCommand { get; set; } = new(nameof(DownCommand), typeof(DreamineTimeSpinner));
247
257 {
258 DefaultStyleKeyProperty.OverrideMetadata(
259 typeof(DreamineTimeSpinner),
260 new FrameworkPropertyMetadata(typeof(DreamineTimeSpinner)));
261
262 var uri = new Uri("/Dreamine.UI.Wpf.Themes;component/DreamineTimeSpinnerStyle.xaml", UriKind.RelativeOrAbsolute);
263
264 if (Application.Current != null)
265 {
266 bool alreadyAdded = Application.Current.Resources.MergedDictionaries
267 .OfType<ResourceDictionary>()
268 .Any(x => x.Source != null && x.Source.Equals(uri));
269
270 if (!alreadyAdded)
271 {
272 var dict = new ResourceDictionary { Source = uri };
273 Application.Current.Resources.MergedDictionaries.Add(dict);
274 }
275 }
276 }
277
287 {
288 CommandBindings.Add(new CommandBinding(UpCommand, OnUpExecuted));
289 CommandBindings.Add(new CommandBinding(DownCommand, OnDownExecuted));
290 }
291
308 public override void OnApplyTemplate()
309 {
310 base.OnApplyTemplate();
311
312 _hourBox = GetTemplateChild(PART_HourBox) as TextBox;
313 _minBox = GetTemplateChild(PART_MinBox) as TextBox;
314 _secBox = GetTemplateChild(PART_SecBox) as TextBox;
315 _upButton = GetTemplateChild(PART_UpButton) as ButtonBase;
316 _downButton = GetTemplateChild(PART_DownButton) as ButtonBase;
317
318 if (_hourBox != null)
319 {
320 _hourBox.GotFocus += (_, __) => _activeBox = eFocusedBox.Hour;
321 _hourBox.LostFocus += HourBox_LostFocus;
322 _hourBox.PreviewKeyDown += HourBox_PreviewKeyDown;
323 }
324 if (_minBox != null)
325 {
326 _minBox.GotFocus += (_, __) => _activeBox = eFocusedBox.Minute;
327 _minBox.LostFocus += MinBox_LostFocus;
328 _minBox.PreviewKeyDown += MinBox_PreviewKeyDown;
329 }
330 if (_secBox != null)
331 {
332 _secBox.GotFocus += (_, __) => _activeBox = eFocusedBox.Second;
333 _secBox.LostFocus += SecBox_LostFocus;
334 _secBox.PreviewKeyDown += SecBox_PreviewKeyDown;
335 }
336
337 if (_upButton != null)
338 _upButton.Command = UpCommand;
339 if (_downButton != null)
340 _downButton.Command = DownCommand;
341
343 }
344
369 private void HourBox_LostFocus(object sender, RoutedEventArgs e) => TryUpdateTimeFromBoxes();
370
395 private void MinBox_LostFocus(object sender, RoutedEventArgs e) => TryUpdateTimeFromBoxes();
396
421 private void SecBox_LostFocus(object sender, RoutedEventArgs e) => TryUpdateTimeFromBoxes();
422
455 private void HourBox_PreviewKeyDown(object sender, KeyEventArgs e)
456 {
457 if (e.Key == Key.Enter) TryUpdateTimeFromBoxes();
458 }
459
492 private void MinBox_PreviewKeyDown(object sender, KeyEventArgs e)
493 {
494 if (e.Key == Key.Enter) TryUpdateTimeFromBoxes();
495 }
496
529 private void SecBox_PreviewKeyDown(object sender, KeyEventArgs e)
530 {
531 if (e.Key == Key.Enter) TryUpdateTimeFromBoxes();
532 }
533
551 {
552 if (_hourBox == null || _minBox == null || _secBox == null) return;
553
554 int h = Clamp(Parse(_hourBox.Text), 0, 23);
555 int m = Clamp(Parse(_minBox.Text), 0, 59);
556 int s = Clamp(Parse(_secBox.Text), 0, 59);
557
558 Time = new TimeSpan(h, m, s);
559
561 }
562
587 private int Parse(string? s)
588 {
589 return int.TryParse(s, out var v) ? v : 0;
590 }
591
632 private int Clamp(int value, int min, int max)
633 {
634 if (value < min) return min;
635 if (value > max) return max;
636 return value;
637 }
638
663 private void OnUpExecuted(object sender, ExecutedRoutedEventArgs e)
664 {
665 switch (_activeBox)
666 {
667 case eFocusedBox.Hour:
668 Time = new TimeSpan((Time.Hours + 1) % 24, Time.Minutes, Time.Seconds);
669 break;
670 case eFocusedBox.Minute:
671 if (Time.Minutes == 59)
672 {
673 int newHour = (Time.Hours + 1) % 24;
674 Time = new TimeSpan(newHour, 0, Time.Seconds);
675 }
676 else
677 {
678 Time = new TimeSpan(Time.Hours, Time.Minutes + 1, Time.Seconds);
679 }
680 break;
681 case eFocusedBox.Second:
682 if (Time.Seconds == 59)
683 {
684 int newMinute = Time.Minutes + 1;
685 int newHour = Time.Hours;
686 if (newMinute == 60)
687 {
688 newMinute = 0;
689 newHour = (newHour + 1) % 24;
690 }
691 Time = new TimeSpan(newHour, newMinute, 0);
692 }
693 else
694 {
695 Time = new TimeSpan(Time.Hours, Time.Minutes, Time.Seconds + 1);
696 }
697 break;
698 }
699 }
700
725 private void OnDownExecuted(object sender, ExecutedRoutedEventArgs e)
726 {
727 switch (_activeBox)
728 {
729 case eFocusedBox.Hour:
730 Time = new TimeSpan((Time.Hours + 23) % 24, Time.Minutes, Time.Seconds);
731 break;
732 case eFocusedBox.Minute:
733 if (Time.Minutes == 0)
734 {
735 int newHour = (Time.Hours + 23) % 24;
736 Time = new TimeSpan(newHour, 59, Time.Seconds);
737 }
738 else
739 {
740 Time = new TimeSpan(Time.Hours, Time.Minutes - 1, Time.Seconds);
741 }
742 break;
743 case eFocusedBox.Second:
744 if (Time.Seconds == 0)
745 {
746 int newMinute = Time.Minutes;
747 int newHour = Time.Hours;
748 if (newMinute == 0)
749 {
750 newMinute = 59;
751 newHour = (newHour + 23) % 24;
752 }
753 else
754 {
755 newMinute = newMinute - 1;
756 }
757 Time = new TimeSpan(newHour, newMinute, 59);
758 }
759 else
760 {
761 Time = new TimeSpan(Time.Hours, Time.Minutes, Time.Seconds - 1);
762 }
763 break;
764 }
765 }
766
775 private void UpdateTextBoxes()
776 {
777 if (_hourBox != null)
778 _hourBox.Text = Time.Hours.ToString("D2");
779 if (_minBox != null)
780 _minBox.Text = Time.Minutes.ToString("D2");
781 if (_secBox != null)
782 _secBox.Text = Time.Seconds.ToString("D2");
783 }
784 }
785}
static void OnTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
void SecBox_LostFocus(object sender, RoutedEventArgs e)
static readonly DependencyProperty TimeProperty
void HourBox_LostFocus(object sender, RoutedEventArgs e)
void MinBox_PreviewKeyDown(object sender, KeyEventArgs e)
void OnDownExecuted(object sender, ExecutedRoutedEventArgs e)
void HourBox_PreviewKeyDown(object sender, KeyEventArgs e)
void SecBox_PreviewKeyDown(object sender, KeyEventArgs e)
void OnUpExecuted(object sender, ExecutedRoutedEventArgs e)
void MinBox_LostFocus(object sender, RoutedEventArgs e)