123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Forms;
- using System.Windows.Input;
- using Microsoft.Xaml.Behaviors;
- using Xceed.Wpf.Toolkit;
- using KeyEventArgs = System.Windows.Input.KeyEventArgs;
- using TextBox = System.Windows.Controls.TextBox;
- namespace InABox.WPF;
- public class TextBoxDateTimeMaskBehavior : Behavior<TextBox>
- {
- private bool bFirst = true;
- private List<Tuple<int, char>> _separators = new List<Tuple<int, char>>();
- private bool _isEnabled = true;
- private bool _previouslyEnabled = true;
-
- private string _format = "";
- public string Format
- {
- get => _format;
- set
- {
- _format = value;
- ReloadSeparators();
- }
- }
- public bool IsEnabled
- {
- get => _isEnabled;
- set
- {
- _isEnabled = value;
- if (AssociatedObject != null)
- AssociatedObject.IsEnabled = value;
- }
- }
-
- private void ReloadSeparators()
- {
- _separators.Clear();
- var formatted = String.Format("{0:"+_format+"}",DateTime.Now );
- int iOffset = 0;
- for (int i=0; i<formatted.Length; i++)
- {
- var ch = formatted[i];
- if (!Char.IsNumber(ch))
- {
- _separators.Add(new Tuple<int, char>(i - iOffset, ch));
- iOffset++;
- }
- }
- }
- public TextBoxDateTimeMaskBehavior(string? format, bool isEnabled = true)
- {
- Format = String.IsNullOrWhiteSpace(format)
- ? "dd/MM/yyyy HH:mm:ss"
- : format;
- _isEnabled = isEnabled;
- }
-
- protected override void OnAttached()
- {
- AssociatedObject.PreviewTextInput += PreviewTextInput;
- AssociatedObject.TextChanged += TextChanged;
- AssociatedObject.MouseDoubleClick += MouseDoubleClick;
- _previouslyEnabled = AssociatedObject.IsEnabled;
- AssociatedObject.IsEnabled = _isEnabled;
- base.OnAttached();
- }
-
- protected override void OnDetaching()
- {
- AssociatedObject.MouseDoubleClick -= MouseDoubleClick;
- AssociatedObject.TextChanged -= TextChanged;
- AssociatedObject.PreviewTextInput -= PreviewTextInput;
- AssociatedObject.IsEnabled = _previouslyEnabled;
- base.OnDetaching();
- }
-
- private void MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- AssociatedObject.Text = String.Format("{0:" + Format + "}", DateTime.Now);
- }
- private void PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- bFirst = false;
- if (!int.TryParse(e.Text, out int _))
- e.Handled = true;
- }
- private void TextChanged(object sender, TextChangedEventArgs e)
- {
- var plaintext = AssociatedObject.Text?.Trim() ?? "";
- foreach (var separator in _separators)
- plaintext = plaintext.Replace(separator.Item2.ToString(), "");
-
- var decorated = plaintext;
- for (int i = _separators.Count - 1; i >= 0; i--)
- {
- if (plaintext.Length >= _separators[i].Item1)
- decorated = decorated.Insert(_separators[i].Item1, _separators[i].Item2.ToString());
- }
- AssociatedObject.Text = decorated;
- if (bFirst)
- AssociatedObject.SelectAll();
- else
- AssociatedObject.Select(AssociatedObject.Text.Length, 0);
- e.Handled = true;
- }
- }
|