CommonWritableComboBox.xaml.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using ImTools;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. using static ImTools.ImMap;
  18. namespace PDF_Office.CustomControl
  19. {
  20. /// <summary>
  21. /// CommonWritableComboBox.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class CommonWritableComboBox : UserControl
  24. {
  25. public CommonWritableComboBox()
  26. {
  27. InitializeComponent();
  28. }
  29. public string Text
  30. {
  31. get { return (string)GetValue(TextProperty); }
  32. set { SetValue(TextProperty, value); }
  33. }
  34. public static readonly DependencyProperty TextProperty =
  35. DependencyProperty.Register("Text", typeof(string), typeof(CommonWritableComboBox), new PropertyMetadata("0"));
  36. /// <summary>
  37. /// 当前值
  38. /// </summary>
  39. public double Value
  40. {
  41. get { return (double)GetValue(ValueProperty); }
  42. set
  43. {
  44. if (value > Maximum)
  45. {
  46. SetValue(ValueProperty, Maximum);
  47. }
  48. else if (value < Minimum)
  49. {
  50. SetValue(ValueProperty, Minimum);
  51. }
  52. else
  53. {
  54. SetValue(ValueProperty, value);
  55. }
  56. }
  57. }
  58. public static readonly DependencyProperty ValueProperty =
  59. DependencyProperty.Register("Value", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(0.0));
  60. public List<string> TypeSouce
  61. {
  62. get { return (List<string>)GetValue(TypeSouceProperty); }
  63. set { SetValue(TypeSouceProperty, value); }
  64. }
  65. public static readonly DependencyProperty TypeSouceProperty =
  66. DependencyProperty.Register("TypeSouce", typeof(List<string>), typeof(CommonWritableComboBox), new PropertyMetadata(new List<string>()));
  67. /// <summary>
  68. /// 最大值
  69. /// </summary>
  70. public double Maximum
  71. {
  72. get { return (double)GetValue(MaximumProperty); }
  73. set { SetValue(MaximumProperty, value); }
  74. }
  75. public static readonly DependencyProperty MaximumProperty =
  76. DependencyProperty.Register("Maximum", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(9999.0));
  77. /// <summary>
  78. /// 最小值
  79. /// </summary>
  80. public double Minimum
  81. {
  82. get { return (double)GetValue(MinimumProperty); }
  83. set { SetValue(MinimumProperty, value); }
  84. }
  85. public static readonly DependencyProperty MinimumProperty =
  86. DependencyProperty.Register("Minimum", typeof(double), typeof(CommonWritableComboBox), new PropertyMetadata(0.0));
  87. /// <summary>
  88. /// 单位符号
  89. /// </summary>
  90. public string Unit
  91. {
  92. get { return (string)GetValue(UnitProperty); }
  93. set { SetValue(UnitProperty, value); }
  94. }
  95. public static readonly DependencyProperty UnitProperty =
  96. DependencyProperty.Register("Unit", typeof(string), typeof(CommonWritableComboBox), new PropertyMetadata("%"));
  97. private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
  98. {
  99. if (this.TextBox_Num.Text == "" )
  100. {
  101. this.TextBox_Num.Text ="0";
  102. }
  103. if (!int.TryParse(this.TextBox_Num.Text, out _)&&!int.TryParse(this.TextBox_Num.Text.Replace(Unit, ""), out _)) { this.TextBox_Num.Text = Text; }
  104. else{
  105. Text = this.TextBox_Num.Text;
  106. Value = int.Parse(this.TextBox_Num.Text.Replace(" ", "").Replace(Unit, ""));
  107. }
  108. }
  109. private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  110. {
  111. this.TextBox_Num.Text = TypeSouce[ComboBox_Type.SelectedIndex];
  112. Text = this.TextBox_Num.Text;
  113. Value = int.Parse(this.TextBox_Num.Text.Replace(" ", "").Replace(Unit, ""));
  114. }
  115. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  116. {
  117. //e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  118. }
  119. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  120. {
  121. if (e.Key == Key.Enter)
  122. {
  123. if (Value > Maximum)
  124. {
  125. this.TextBox_Num.Text = Maximum.ToString();
  126. }
  127. else if (Value < Minimum)
  128. {
  129. this.TextBox_Num.Text = Minimum.ToString();
  130. }
  131. else
  132. {
  133. this.TextBox_Num.Text=Value.ToString();
  134. }
  135. this.TextBox_Num.Text = this.TextBox_Num.Text.Replace(" " , "").Replace(Unit,"")+" "+Unit;
  136. }
  137. }
  138. }
  139. }