/SoftGetaway.Client/gui/MyTextBox.xaml.cs

https://gitlab.com/prog76.own/SoftGetaway · C# · 131 lines · 111 code · 16 blank · 4 comment · 12 complexity · 3a3d7d1dd273c7d4b4dcd74ab970bdd0 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.ComponentModel;
  15. using System.Text.RegularExpressions;
  16. namespace softGetawayClient.gui {
  17. /// <summary>
  18. /// Interaction logic for MyTextBox.xaml
  19. /// </summary>
  20. public partial class MyTextBox : UserControl, INotifyPropertyChanged {
  21. Regex rex;
  22. bool fIsValid;
  23. public MyTextBox() {
  24. InitializeComponent();
  25. rex = new Regex(".*");
  26. maskedBox.TextChanged += new TextChangedEventHandler(maskedBox_TextChanged);
  27. DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBox.IsEnabledProperty, typeof(TextBox));
  28. if (dpd != null)
  29. dpd.AddValueChanged(maskedBox, delegate {
  30. setBackground();
  31. OnPropertyChanged(new PropertyChangedEventArgs("IsValidOrDisabled"));
  32. });
  33. }
  34. void maskedBox_TextChanged(object sender, TextChangedEventArgs e) {
  35. bool _IsValid = rex.IsMatch(maskedBox.Text);
  36. if (_IsValid != fIsValid) {
  37. fIsValid = _IsValid;
  38. labl.FontSize = maskedBox.FontSize + (fIsValid ? 5 : 2);
  39. labl.Measure(new Size(Double.MaxValue, Double.MaxValue));
  40. Size lablSize = labl.DesiredSize;
  41. Rect rect = new Rect(0, (lablSize.Height - maskedBox.ActualHeight) / 2, lablSize.Width, maskedBox.ActualHeight);
  42. labl.Clip = new RectangleGeometry(rect);
  43. OnPropertyChanged(new PropertyChangedEventArgs("IsValid"));
  44. OnPropertyChanged(new PropertyChangedEventArgs("IsValidOrDisabled"));
  45. if (!fIsValid)
  46. toolTip.Content = ErrorMessage;
  47. toolTip.IsOpen = !fIsValid;
  48. }
  49. }
  50. public event TextChangedEventHandler TextChanged {
  51. add {
  52. maskedBox.TextChanged += value;
  53. }
  54. remove {
  55. maskedBox.TextChanged -= value;
  56. }
  57. }
  58. public bool IsEditable {
  59. get { return (bool)GetValue(IsEditableProperty); }
  60. set { SetValue(IsEditableProperty, value); }
  61. }
  62. public String Mask {
  63. get { return rex.ToString(); }
  64. set { rex = new Regex(value); }
  65. }
  66. public String ErrorMessage {
  67. get { return (string)GetValue(ErrorMessageProperty); }
  68. set { SetValue(ErrorMessageProperty, value); }
  69. }
  70. public String Text {
  71. get { return maskedBox.Text; }
  72. set { maskedBox.Text = value; }
  73. }
  74. public String ValidTextOrNull
  75. {
  76. get { return IsValid ? Text : null; }
  77. }
  78. public bool IsValid {
  79. get { return fIsValid; }
  80. }
  81. public bool IsValidOrDisabled {
  82. get { return !IsEditable || !IsEnabled || fIsValid; }
  83. }
  84. void setBackground() {
  85. if (IsEditable && maskedBox.IsEnabled)
  86. Background = SystemColors.ControlLightLightBrush;
  87. else
  88. if (IsEnabled)
  89. Background = SystemColors.ControlLightBrush;
  90. else
  91. Background = SystemColors.ControlBrush;
  92. }
  93. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  94. public static readonly DependencyProperty IsEditableProperty =
  95. DependencyProperty.Register("IsEditable", typeof(bool), typeof(MyTextBox),
  96. new UIPropertyMetadata(true, new PropertyChangedCallback(IsEditableChangedCallback)));
  97. private static void IsEditableChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  98. var control = d as MyTextBox;
  99. control.IsEditableChangedCallback((bool)e.OldValue, (bool)e.NewValue);
  100. }
  101. private void IsEditableChangedCallback(bool oldValue, bool newValue) {
  102. maskedBox.IsReadOnly = !newValue;
  103. setBackground();
  104. }
  105. public static readonly DependencyProperty ErrorMessageProperty =
  106. DependencyProperty.Register("ErrorMessage", typeof(string), typeof(MyTextBox),
  107. new UIPropertyMetadata("", null));
  108. public event PropertyChangedEventHandler PropertyChanged;
  109. public void OnPropertyChanged(PropertyChangedEventArgs e) {
  110. if (PropertyChanged != null) {
  111. PropertyChanged(this, e);
  112. }
  113. }
  114. }
  115. }