TextboxWithLabel.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace CyberX8_Themes.UserControls
  17. {
  18. /// <summary>
  19. /// TextboxWithLabel.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class TextboxWithLabel : UserControl
  22. {
  23. public TextboxWithLabel()
  24. {
  25. InitializeComponent();
  26. }
  27. public static readonly DependencyProperty LabelValueProperty = DependencyProperty.Register(
  28. "LabelValue", typeof(string), typeof(TextboxWithLabel));
  29. public string LabelValue
  30. {
  31. get { return (string)this.GetValue(LabelValueProperty); }
  32. set { this.SetValue(LabelValueProperty, value); }
  33. }
  34. public static readonly DependencyProperty TextBoxValueProperty = DependencyProperty.Register(
  35. "TextBoxValue", typeof(string), typeof(TextboxWithLabel));
  36. public string TextBoxValue
  37. {
  38. get { return (string)this.GetValue(TextBoxValueProperty); }
  39. set { this.SetValue(TextBoxValueProperty, value); }
  40. }
  41. public static readonly DependencyProperty TextBoxColorProperty = DependencyProperty.Register(
  42. "TextBoxColor", typeof(SolidColorBrush), typeof(TextboxWithLabel));
  43. public SolidColorBrush TextBoxColor
  44. {
  45. get { return (SolidColorBrush)this.GetValue(TextBoxColorProperty); }
  46. set { this.SetValue(TextBoxColorProperty, value); }
  47. }
  48. public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
  49. "IsReadOnly", typeof(bool), typeof(TextboxWithLabel),new PropertyMetadata (true));
  50. public bool IsReadOnly
  51. {
  52. get
  53. {
  54. return (bool)this.GetValue(IsReadOnlyProperty);
  55. }
  56. set
  57. {
  58. this.SetValue(IsReadOnlyProperty, value);
  59. }
  60. }
  61. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
  62. "MaxValue", typeof(int), typeof(TextboxWithLabel),new PropertyMetadata(OnTextBoxPropertyChanged));
  63. private static void OnTextBoxPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  64. {
  65. TextboxWithLabel textboxWithLabel = d as TextboxWithLabel;
  66. if (textboxWithLabel.tb == null)
  67. {
  68. return;
  69. }
  70. textboxWithLabel.tb.Tag = e.NewValue;
  71. textboxWithLabel.tb.TextChanged += Tb_TextChanged;
  72. }
  73. private static void Tb_TextChanged(object sender, TextChangedEventArgs e)
  74. {
  75. TextBox tb = sender as TextBox;
  76. if (tb.Text == "")
  77. {
  78. return;
  79. }
  80. string str = tb.Text;
  81. str = Regex.Replace(str, @"[^\d.\d]", "");
  82. if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
  83. {
  84. decimal result;
  85. if (decimal.TryParse(str, out result) == false)
  86. {
  87. tb.Text = "";
  88. return;
  89. }
  90. if (result > (int)tb.Tag)
  91. {
  92. tb.Text = tb.Tag.ToString();
  93. }
  94. }
  95. }
  96. public int MaxValue
  97. {
  98. get { return (int)this.GetValue(MaxValueProperty); }
  99. set { this.SetValue(MaxValueProperty, value); }
  100. }
  101. }
  102. }