AITHeaterInputDialogBox.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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.Globalization;
  15. using Aitex.Core.RT.Log;
  16. namespace Aitex.Core.UI.DeviceControl
  17. {
  18. public class DoubleConverter : IValueConverter
  19. {
  20. public DoubleConverter()
  21. {
  22. }
  23. // Summary:
  24. // Converts a value.
  25. //
  26. // Parameters:
  27. // value:
  28. // The value produced by the binding source.
  29. //
  30. // targetType:
  31. // The type of the binding target property.
  32. //
  33. // parameter:
  34. // The converter parameter to use.
  35. //
  36. // culture:
  37. // The culture to use in the converter.
  38. //
  39. // Returns:
  40. // A converted value. If the method returns null, the valid null value is used.
  41. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  42. {
  43. return value.ToString();
  44. }
  45. //
  46. // Summary:
  47. // Converts a value.
  48. //
  49. // Parameters:
  50. // value:
  51. // The value that is produced by the binding target.
  52. //
  53. // targetType:
  54. // The type to convert to.
  55. //
  56. // parameter:
  57. // The converter parameter to use.
  58. //
  59. // culture:
  60. // The culture to use in the converter.
  61. //
  62. // Returns:
  63. // A converted value. If the method returns null, the valid null value is used.
  64. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  65. {
  66. string input = (string)value;
  67. double result = 0;
  68. if (double.TryParse(input, out result))
  69. {
  70. return result;
  71. }
  72. return DependencyProperty.UnsetValue;
  73. }
  74. }
  75. public class InputDialogValidationRule : ValidationRule
  76. {
  77. double minInput;
  78. double maxInput;
  79. public double MinInput
  80. {
  81. get
  82. {
  83. return this.minInput;
  84. }
  85. set
  86. {
  87. this.minInput = value;
  88. }
  89. }
  90. public double MaxInput
  91. {
  92. get
  93. {
  94. return this.maxInput;
  95. }
  96. set
  97. {
  98. this.maxInput = value;
  99. }
  100. }
  101. public InputDialogValidationRule()
  102. {
  103. }
  104. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  105. {
  106. double result;
  107. // Is a number?
  108. if (!double.TryParse((string)value, out result))
  109. {
  110. string msg = string.Format("‘{0}’不是一个数。", value);
  111. return new ValidationResult(false, msg);
  112. }
  113. // Is in range?
  114. if ((result < this.minInput) || (result > this.maxInput))
  115. {
  116. string msg = string.Format("输入值必须在‘{0}’与‘{1}’之间。", this.minInput, this.maxInput);
  117. return new ValidationResult(false, msg);
  118. }
  119. // Number is valid
  120. return new ValidationResult(true, null);
  121. }
  122. }
  123. /// <summary>
  124. /// Interaction logic for InputDialogBox.xaml
  125. /// </summary>
  126. public partial class AITHeaterInputDialogBox : Window
  127. {
  128. public AITHeaterInputDialogBox()
  129. {
  130. InitializeComponent();
  131. DataContext = this;
  132. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  133. }
  134. public void FocasAll()
  135. {
  136. inputBox.Text = Math.Round(SetPoint, 2).ToString();
  137. inputBox.Focus();
  138. inputBox.SelectAll();
  139. }
  140. /// <summary>
  141. /// Vilidate input range
  142. /// </summary>
  143. /// <param name="sender"></param>
  144. /// <param name="e"></param>
  145. private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
  146. {
  147. double input;
  148. if (!double.TryParse(inputBox.Text, out input))
  149. btnSet.IsEnabled = false;
  150. else if (input < 0 || input > MaxValue || input < MinValue)
  151. btnSet.IsEnabled = false;
  152. else
  153. btnSet.IsEnabled = true;
  154. inputBox.Foreground = btnSet.IsEnabled ?
  155. System.Windows.Media.Brushes.Black : System.Windows.Media.Brushes.Red;
  156. }
  157. public static readonly DependencyProperty DeviceNameProperty = DependencyProperty.Register(
  158. "DeviceName", typeof(string), typeof(AITHeaterInputDialogBox),
  159. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  160. public static readonly DependencyProperty DeviceIdProperty = DependencyProperty.Register(
  161. "DeviceId", typeof(string), typeof(AITHeaterInputDialogBox),
  162. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  163. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
  164. "MaxValue", typeof(double), typeof(AITHeaterInputDialogBox),
  165. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  166. public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register(
  167. "MinValue", typeof(double), typeof(AITHeaterInputDialogBox),
  168. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  169. public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
  170. "DefaultValue", typeof(double), typeof(AITHeaterInputDialogBox),
  171. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  172. public static readonly DependencyProperty RealValueProperty = DependencyProperty.Register(
  173. "RealValue", typeof(string), typeof(AITHeaterInputDialogBox),
  174. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  175. public static readonly DependencyProperty HeaterOnProperty = DependencyProperty.Register(
  176. "HeaterOn", typeof(string), typeof(AITHeaterInputDialogBox),
  177. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  178. public static readonly DependencyProperty HeaterOffProperty = DependencyProperty.Register(
  179. "HeaterOff", typeof(string), typeof(AITHeaterInputDialogBox),
  180. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  181. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
  182. "Unit", typeof(string), typeof(AITHeaterInputDialogBox),
  183. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  184. public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
  185. "SetPoint", typeof(double), typeof(AITHeaterInputDialogBox),
  186. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  187. public static readonly DependencyProperty IsHeaterOnProperty = DependencyProperty.Register(
  188. "IsHeaterOn", typeof(bool), typeof(AITHeaterInputDialogBox),
  189. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  190. /// <summary>
  191. /// 是否百分比显示
  192. /// </summary>
  193. public bool IsPercent
  194. {
  195. get;
  196. set;
  197. }
  198. public string DeviceName
  199. {
  200. get
  201. {
  202. return (string)this.GetValue(DeviceNameProperty);
  203. }
  204. set
  205. {
  206. if (!string.IsNullOrEmpty(value) && !value.StartsWith("_"))
  207. value = "_" + value;
  208. this.SetValue(DeviceNameProperty, value);
  209. }
  210. }
  211. public string DeviceId
  212. {
  213. get
  214. {
  215. return (string)this.GetValue(DeviceIdProperty);
  216. }
  217. set
  218. {
  219. this.SetValue(DeviceIdProperty, value);
  220. }
  221. }
  222. public double MaxValue
  223. {
  224. get
  225. {
  226. return (double)this.GetValue(MaxValueProperty);
  227. }
  228. set
  229. {
  230. this.SetValue(MaxValueProperty, value);
  231. // validationRule.MaxInput = value;
  232. }
  233. }
  234. public int MinValueHeight
  235. {
  236. get
  237. {
  238. if (DeviceId == "Chiller") return 30;
  239. else return 0;
  240. }
  241. }
  242. public double MinValue
  243. {
  244. get
  245. {
  246. return (double)this.GetValue(MinValueProperty);
  247. }
  248. set
  249. {
  250. this.SetValue(MinValueProperty, value);
  251. // validationRule.MaxInput = value;
  252. }
  253. }
  254. public double DefaultValue
  255. {
  256. get
  257. {
  258. return (double)this.GetValue(DefaultValueProperty);
  259. }
  260. set
  261. {
  262. this.SetValue(DefaultValueProperty, value);
  263. }
  264. }
  265. public string RealValue
  266. {
  267. get
  268. {
  269. return (string)this.GetValue(RealValueProperty);
  270. }
  271. set
  272. {
  273. this.SetValue(RealValueProperty, value);
  274. }
  275. }
  276. public string HeaterOnName
  277. {
  278. get
  279. {
  280. return (string)this.GetValue(HeaterOnProperty);
  281. }
  282. set
  283. {
  284. this.SetValue(HeaterOnProperty, value);
  285. }
  286. }
  287. public string HeaterOffName
  288. {
  289. get
  290. {
  291. return (string)this.GetValue(HeaterOffProperty);
  292. }
  293. set
  294. {
  295. this.SetValue(HeaterOffProperty, value);
  296. }
  297. }
  298. public string Unit
  299. {
  300. get
  301. {
  302. return (string)this.GetValue(UnitProperty);
  303. }
  304. set
  305. {
  306. this.SetValue(UnitProperty, value);
  307. }
  308. }
  309. public double SetPoint
  310. {
  311. get
  312. {
  313. return (double)this.GetValue(SetPointProperty);
  314. }
  315. set
  316. {
  317. this.SetValue(SetPointProperty, value);
  318. }
  319. }
  320. public bool IsHeaterOn
  321. {
  322. get
  323. {
  324. return (bool)this.GetValue(IsHeaterOnProperty);
  325. }
  326. set
  327. {
  328. this.SetValue(IsHeaterOnProperty, value);
  329. btnHeaterOff.IsEnabled = value;
  330. btnHeaterOn.IsEnabled = !value;
  331. }
  332. }
  333. public Action<bool> SetHeaterPowerOnOffCommandDelegate;
  334. public Action<double> SetHeaterValueCommandDelegate;
  335. private void ButtonSet_Click(object sender, RoutedEventArgs e)
  336. {
  337. try
  338. {
  339. SetHeaterValueCommandDelegate(Convert.ToDouble(inputBox.Text));
  340. Close();
  341. }
  342. catch (Exception ex)
  343. {
  344. LOG.Error(ex.Message);
  345. }
  346. }
  347. private void OnEnterKeyIsHit(object sender, KeyEventArgs e)
  348. {
  349. try
  350. {
  351. if (!btnSet.IsEnabled)
  352. return;
  353. if (e.Key == Key.Return)
  354. {
  355. ButtonSet_Click(null, null);
  356. }
  357. }
  358. catch (Exception ex)
  359. {
  360. LOG.Error(ex.Message);
  361. }
  362. }
  363. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  364. {
  365. Close();
  366. }
  367. private void ButtonHeaterOn_Click(object sender, RoutedEventArgs e)
  368. {
  369. try
  370. {
  371. if (SetHeaterPowerOnOffCommandDelegate != null)
  372. {
  373. SetHeaterPowerOnOffCommandDelegate(true);
  374. }
  375. }
  376. catch (Exception ex)
  377. {
  378. LOG.Error(ex.Message);
  379. }
  380. }
  381. private void ButtonHeaterOff_Click(object sender, RoutedEventArgs e)
  382. {
  383. try
  384. {
  385. if (SetHeaterPowerOnOffCommandDelegate != null)
  386. {
  387. SetHeaterPowerOnOffCommandDelegate(false);
  388. }
  389. }
  390. catch (Exception ex)
  391. {
  392. LOG.Error(ex.Message);
  393. }
  394. }
  395. }
  396. }