InputDialogBox.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using Aitex.Core.RT.Log;
  2. using System;
  3. using System.Globalization;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Input;
  8. namespace Aitex.Core.UI.Control
  9. {
  10. public class DoubleConverter : IValueConverter
  11. {
  12. public DoubleConverter()
  13. {
  14. }
  15. // Summary:
  16. // Converts a value.
  17. //
  18. // Parameters:
  19. // value:
  20. // The value produced by the binding source.
  21. //
  22. // targetType:
  23. // The type of the binding target property.
  24. //
  25. // parameter:
  26. // The converter parameter to use.
  27. //
  28. // culture:
  29. // The culture to use in the converter.
  30. //
  31. // Returns:
  32. // A converted value. If the method returns null, the valid null value is used.
  33. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  34. {
  35. return value.ToString();
  36. }
  37. //
  38. // Summary:
  39. // Converts a value.
  40. //
  41. // Parameters:
  42. // value:
  43. // The value that is produced by the binding target.
  44. //
  45. // targetType:
  46. // The type to convert to.
  47. //
  48. // parameter:
  49. // The converter parameter to use.
  50. //
  51. // culture:
  52. // The culture to use in the converter.
  53. //
  54. // Returns:
  55. // A converted value. If the method returns null, the valid null value is used.
  56. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  57. {
  58. string input = (string)value;
  59. double result = 0;
  60. if (double.TryParse(input, out result))
  61. {
  62. return result;
  63. }
  64. return DependencyProperty.UnsetValue;
  65. }
  66. }
  67. public class InputDialogValidationRule : ValidationRule
  68. {
  69. double minInput;
  70. double maxInput;
  71. public double MinInput
  72. {
  73. get { return this.minInput; }
  74. set { this.minInput = value; }
  75. }
  76. public double MaxInput
  77. {
  78. get { return this.maxInput; }
  79. set { this.maxInput = value; }
  80. }
  81. public InputDialogValidationRule()
  82. {
  83. }
  84. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  85. {
  86. double result;
  87. // Is a number?
  88. if (!double.TryParse((string)value, out result))
  89. {
  90. string msg = string.Format("‘{0}’不是一个数.", value);
  91. return new ValidationResult(false, msg);
  92. }
  93. // Is in range?
  94. if ((result < this.minInput) || (result > this.maxInput))
  95. {
  96. string msg = string.Format("输入值必须在‘{0}’与‘{1}’之间.", this.minInput, this.maxInput);
  97. return new ValidationResult(false, msg);
  98. }
  99. // Number is valid
  100. return new ValidationResult(true, null);
  101. }
  102. }
  103. /// <summary>
  104. /// Interaction logic for InputDialogBox.xaml
  105. /// </summary>
  106. public partial class InputDialogBox : Window, IDisposable
  107. {
  108. private bool disposed = false;
  109. public InputDialogBox()
  110. {
  111. InitializeComponent();
  112. DataContext = this;
  113. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  114. }
  115. public void FocasAll()
  116. {
  117. inputBox.Text = Math.Round(SetPoint, 2).ToString();
  118. inputBox.Focus();
  119. inputBox.SelectAll();
  120. }
  121. private bool setPointIsError = false;
  122. /// <summary>
  123. /// Vilidate input range
  124. /// </summary>
  125. /// <param name="sender"></param>
  126. /// <param name="e"></param>
  127. private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
  128. {
  129. double input;
  130. if (!double.TryParse(inputBox.Text, out input)) setPointIsError = true;
  131. else if (input < 0 || input > MaxValue) setPointIsError = true;
  132. else setPointIsError = false;
  133. inputBox.Foreground = !setPointIsError ?
  134. System.Windows.Media.Brushes.Black : System.Windows.Media.Brushes.Red;
  135. buttonSet.IsEnabled = !rampIsError && !setPointIsError;
  136. }
  137. private bool rampIsError = false;
  138. private void inputRampBox_TextChanged(object sender, TextChangedEventArgs e)
  139. {
  140. double input;
  141. if (!double.TryParse(inputRampBox.Text, out input)) rampIsError = true;
  142. else if (input < RampMinValue || input > RampMaxValue) rampIsError = true;
  143. else rampIsError = false;
  144. inputRampBox.Foreground = !rampIsError ? System.Windows.Media.Brushes.Black : System.Windows.Media.Brushes.Red;
  145. buttonSet.IsEnabled = !rampIsError && !setPointIsError;
  146. }
  147. public static readonly DependencyProperty DeviceNameProperty = DependencyProperty.Register(
  148. "DeviceName", typeof(string), typeof(InputDialogBox),
  149. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  150. public static readonly DependencyProperty DeviceIdProperty = DependencyProperty.Register(
  151. "DeviceId", typeof(string), typeof(InputDialogBox),
  152. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  153. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
  154. "MaxValue", typeof(double), typeof(InputDialogBox),
  155. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  156. public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
  157. "DefaultValue", typeof(double), typeof(InputDialogBox),
  158. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  159. public static readonly DependencyProperty RealValueProperty = DependencyProperty.Register(
  160. "RealValue", typeof(string), typeof(InputDialogBox),
  161. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  162. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
  163. "Unit", typeof(string), typeof(InputDialogBox),
  164. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  165. public static readonly DependencyProperty TagNameProperty = DependencyProperty.Register(
  166. "TagName", typeof(string), typeof(InputDialogBox),
  167. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  168. public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
  169. "SetPoint", typeof(double), typeof(InputDialogBox),
  170. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  171. public static readonly DependencyProperty RampValueProperty = DependencyProperty.Register(
  172. "RampValue", typeof(double), typeof(InputDialogBox),
  173. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  174. public static readonly DependencyProperty RampMaxValueProperty = DependencyProperty.Register(
  175. "RampMaxValue", typeof(double), typeof(InputDialogBox),
  176. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  177. public static readonly DependencyProperty RampMinValueProperty = DependencyProperty.Register(
  178. "RampMinValue", typeof(double), typeof(InputDialogBox),
  179. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  180. public static readonly DependencyProperty RampVisibilityProperty = DependencyProperty.Register(
  181. "RampVisibility", typeof(Visibility), typeof(InputDialogBox),
  182. new FrameworkPropertyMetadata(Visibility.Collapsed, FrameworkPropertyMetadataOptions.AffectsRender));
  183. /// <summary>
  184. /// 是否百分比显示
  185. /// </summary>
  186. public bool IsPercent { get; set; }
  187. public string DeviceName
  188. {
  189. get
  190. {
  191. return (string)this.GetValue(DeviceNameProperty);
  192. }
  193. set
  194. {
  195. if (!string.IsNullOrEmpty(value) && !value.StartsWith("_"))
  196. value = "_" + value;
  197. this.SetValue(DeviceNameProperty, value);
  198. }
  199. }
  200. public string DeviceTitle
  201. {
  202. get
  203. {
  204. return string.Format("{0} {1}", Application.Current.Resources["GlobalLableSetPointTitle"], DeviceId);
  205. }
  206. }
  207. public string DeviceId
  208. {
  209. get
  210. {
  211. return (string)this.GetValue(DeviceIdProperty);
  212. }
  213. set
  214. {
  215. this.SetValue(DeviceIdProperty, value);
  216. }
  217. }
  218. public double MaxValue
  219. {
  220. get
  221. {
  222. return (double)this.GetValue(MaxValueProperty);
  223. }
  224. set
  225. {
  226. this.SetValue(MaxValueProperty, value);
  227. // validationRule.MaxInput = value;
  228. }
  229. }
  230. public double DefaultValue
  231. {
  232. get
  233. {
  234. return (double)this.GetValue(DefaultValueProperty);
  235. }
  236. set
  237. {
  238. this.SetValue(DefaultValueProperty, value);
  239. }
  240. }
  241. public string RealValue
  242. {
  243. get
  244. {
  245. return (string)this.GetValue(RealValueProperty);
  246. }
  247. set
  248. {
  249. this.SetValue(RealValueProperty, value);
  250. }
  251. }
  252. public string Unit
  253. {
  254. get
  255. {
  256. return (string)this.GetValue(UnitProperty);
  257. }
  258. set
  259. {
  260. this.SetValue(UnitProperty, value);
  261. }
  262. }
  263. public string TagName
  264. {
  265. get
  266. {
  267. return (string)this.GetValue(TagNameProperty);
  268. }
  269. set
  270. {
  271. this.SetValue(TagNameProperty, value);
  272. }
  273. }
  274. public double SetPoint
  275. {
  276. get
  277. {
  278. return (double)this.GetValue(SetPointProperty);
  279. }
  280. set
  281. {
  282. this.SetValue(SetPointProperty, value);
  283. }
  284. }
  285. public double RampValue
  286. {
  287. get
  288. {
  289. return (double)this.GetValue(RampValueProperty);
  290. }
  291. set
  292. {
  293. this.SetValue(RampValueProperty, value);
  294. }
  295. }
  296. public double RampMaxValue
  297. {
  298. get
  299. {
  300. return (double)this.GetValue(RampMaxValueProperty);
  301. }
  302. set
  303. {
  304. this.SetValue(RampMaxValueProperty, value);
  305. }
  306. }
  307. public double RampMinValue
  308. {
  309. get
  310. {
  311. return (double)this.GetValue(RampMinValueProperty);
  312. }
  313. set
  314. {
  315. this.SetValue(RampMinValueProperty, value);
  316. }
  317. }
  318. public Visibility RampVisibility
  319. {
  320. get
  321. {
  322. return (Visibility)this.GetValue(RampVisibilityProperty);
  323. }
  324. set
  325. {
  326. this.SetValue(RampVisibilityProperty, value);
  327. }
  328. }
  329. public Action<double, double?> CommandDelegate;
  330. private void ButtonSet_Click(object sender, RoutedEventArgs e)
  331. {
  332. try
  333. {
  334. if (CommandDelegate != null)
  335. {
  336. if (double.TryParse(inputBox.Text, out double setp))
  337. {
  338. if (IsPercent)
  339. {
  340. setp = setp / 100.0;
  341. }
  342. if (RampVisibility == Visibility.Visible)
  343. {
  344. if (double.TryParse(inputRampBox.Text, out double ramp))
  345. {
  346. CommandDelegate(setp, ramp);
  347. }
  348. else
  349. {
  350. CommandDelegate(setp, null);
  351. }
  352. }
  353. else
  354. {
  355. CommandDelegate(setp, null);
  356. }
  357. }
  358. }
  359. e.Handled = true;
  360. this.DialogResult = true;
  361. Close();
  362. Dispose();
  363. }
  364. catch (Exception ex)
  365. {
  366. LOG.Error(ex.Message);
  367. }
  368. }
  369. private void OnEnterKeyIsHit(object sender, KeyEventArgs e)
  370. {
  371. try
  372. {
  373. if (!buttonSet.IsEnabled) return;
  374. if (e.Key == Key.Return)
  375. {
  376. ButtonSet_Click(null, null);
  377. }
  378. }
  379. catch (Exception ex)
  380. {
  381. LOG.Error(ex.Message);
  382. }
  383. }
  384. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  385. {
  386. e.Handled = true;
  387. this.DialogResult = false;
  388. Close();
  389. Dispose();
  390. }
  391. private void button_PreviewTouchUp(object sender, TouchEventArgs e)
  392. {
  393. try
  394. {
  395. if (CommandDelegate != null)
  396. {
  397. if (double.TryParse(inputBox.Text, out double setp))
  398. {
  399. if (IsPercent)
  400. {
  401. setp = setp / 100.0;
  402. }
  403. if (RampVisibility == Visibility.Visible)
  404. {
  405. if (double.TryParse(inputRampBox.Text, out double ramp))
  406. {
  407. CommandDelegate(setp, ramp);
  408. }
  409. else
  410. {
  411. CommandDelegate(setp, null);
  412. }
  413. }
  414. else
  415. {
  416. CommandDelegate(setp, null);
  417. }
  418. }
  419. }
  420. e.Handled = true;
  421. this.DialogResult = true;
  422. Close();
  423. Dispose();
  424. }
  425. catch (Exception ex)
  426. {
  427. LOG.Error(ex.Message);
  428. }
  429. }
  430. private void button_PreviewCancelTouchUp(object sender, TouchEventArgs e)
  431. {
  432. e.Handled = true;
  433. this.DialogResult = false;
  434. Close();
  435. Dispose();
  436. }
  437. public void Dispose()
  438. {
  439. Dispose(true);
  440. GC.SuppressFinalize(this); // 防止垃圾回收器再次调用析构函数
  441. }
  442. protected virtual void Dispose(bool disposing)
  443. {
  444. if (!disposed)
  445. {
  446. if (disposing)
  447. {
  448. // 释放托管资源
  449. }
  450. // 释放非托管资源
  451. disposed = true;
  452. }
  453. }
  454. }
  455. }