37 InitializeComponent();
42 private bool _digitEntry_StartWithFirstDigit =
true;
43 private byte _digitEntry_CountEnteredZeroes = 0;
47 #region Dependency Properties
63 private static void OnEditModeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
66 control._digitEntry_StartWithFirstDigit =
true;
67 control._digitEntry_CountEnteredZeroes = 0;
84 public static readonly DependencyProperty
ValueProperty = DependencyProperty.Register(nameof(
Value), typeof(TimeSpan), typeof(
TimeSpanControl),
new FrameworkPropertyMetadata(TimeSpan.Zero, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
new PropertyChangedCallback(OnValueChanged)));
86 private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
89 control.Hours = ((TimeSpan)e.NewValue).
Hours;
90 control.Minutes = ((TimeSpan)e.NewValue).
Minutes;
91 control.Seconds = ((TimeSpan)e.NewValue).
Seconds;
92 control.Milliseconds = ((TimeSpan)e.NewValue).
Milliseconds;
109 public static readonly DependencyProperty
HoursProperty = DependencyProperty.Register(nameof(
Hours), typeof(
int), typeof(
TimeSpanControl),
new UIPropertyMetadata(0,
new PropertyChangedCallback(OnTimeChanged)));
123 public static readonly DependencyProperty
MinutesProperty = DependencyProperty.Register(nameof(
Minutes), typeof(
int), typeof(
TimeSpanControl),
new UIPropertyMetadata(0,
new PropertyChangedCallback(OnTimeChanged)));
137 public static readonly DependencyProperty
SecondsProperty = DependencyProperty.Register(nameof(
Seconds), typeof(
int), typeof(
TimeSpanControl),
new UIPropertyMetadata(0,
new PropertyChangedCallback(OnTimeChanged)));
153 private static void OnTimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
157 control.Value =
new TimeSpan(0, control.Hours, control.Minutes, control.Seconds, control.Milliseconds);
158 control.updateMillisecondsDisplayed();
178 private static void OnMillisecondDigitsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
182 if(control.MillisecondDigits < 1) { control.MillisecondDigits = 1; }
184 control.updateMillisecondsDisplayed();
197 private static readonly DependencyPropertyKey MillisecondsDisplayedPropertyKey = DependencyProperty.RegisterReadOnly(nameof(
MillisecondsDisplayed), typeof(
string), typeof(
TimeSpanControl),
new PropertyMetadata(
"000"));
204 private void updateMillisecondsDisplayed()
209 string formattedMilliseconds = displayedMilliseconds.ToString(format);
210 SetValue(MillisecondsDisplayedPropertyKey, formattedMilliseconds);
243 private static void OnVisiblePartsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
260 #region Digit Update Logic
272 if(digit > 9) { digit = 9; }
274 int oldValueMultiplier = _digitEntry_StartWithFirstDigit ? 0 : 10;
275 _digitEntry_StartWithFirstDigit =
false;
276 if(digit == 0) { _digitEntry_CountEnteredZeroes++; }
295 if (
Seconds * 10 >= 59 || _digitEntry_CountEnteredZeroes >= 2) { _digitEntry_StartWithFirstDigit =
true; _digitEntry_CountEnteredZeroes = 0; }
300 int maxValue = 1000 - factor;
302 int tmpMilliseconds = (((
Milliseconds / factor) * oldValueMultiplier) + digit) * factor;
307 _digitEntry_StartWithFirstDigit =
true;
308 _digitEntry_CountEnteredZeroes = 0;
332 int max = 1000 - step;
379 #region Control Events
381 private void txt_hours_PreviewMouseDown(
object sender, MouseButtonEventArgs e)
388 private void txt_minutes_PreviewMouseDown(
object sender, MouseButtonEventArgs e)
395 private void txt_seconds_PreviewMouseDown(
object sender, MouseButtonEventArgs e)
402 private void txt_milliseconds_PreviewMouseDown(
object sender, MouseButtonEventArgs e)
409 private void UserControl_PreviewKeyDown(
object sender, KeyEventArgs e)
411 if (e.Key == Key.Tab)
416 else if ((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
418 string keyString = e.Key.ToString().Replace(
"NumPad",
"").Replace(
"D",
"");
420 if (
byte.TryParse(keyString, out digit))
425 else if (e.Key == Key.Right)
434 else if (e.Key == Key.Left)
443 else if(e.Key == Key.Up)
447 else if (e.Key == Key.Down)
451 else if (e.Key == Key.Enter)
454 Keyboard.ClearFocus();
459 private void btn_Increase_Click(
object sender, RoutedEventArgs e)
464 private void btn_Decrease_Click(
object sender, RoutedEventArgs e)