AITHeaterInputDialogBox.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 static readonly DependencyProperty DeviceNameProperty = DependencyProperty.Register(
  129. "DeviceName", typeof(string), typeof(AITHeaterInputDialogBox),
  130. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  131. public static readonly DependencyProperty DeviceIdProperty = DependencyProperty.Register(
  132. "DeviceId", typeof(string), typeof(AITHeaterInputDialogBox),
  133. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  134. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
  135. "MaxValue", typeof(double), typeof(AITHeaterInputDialogBox),
  136. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  137. public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
  138. "DefaultValue", typeof(double), typeof(AITHeaterInputDialogBox),
  139. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  140. public static readonly DependencyProperty RealValueProperty = DependencyProperty.Register(
  141. "RealValue", typeof(string), typeof(AITHeaterInputDialogBox),
  142. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  143. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
  144. "Unit", typeof(string), typeof(AITHeaterInputDialogBox),
  145. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  146. public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
  147. "SetPoint", typeof(double), typeof(AITHeaterInputDialogBox),
  148. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  149. public static readonly DependencyProperty IsHeaterOnProperty = DependencyProperty.Register(
  150. "IsHeaterOn", typeof(bool), typeof(AITHeaterInputDialogBox),
  151. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  152. /// <summary>
  153. /// 是否百分比显示
  154. /// </summary>
  155. public bool IsPercent
  156. {
  157. get;
  158. set;
  159. }
  160. public string DeviceName
  161. {
  162. get
  163. {
  164. return (string)this.GetValue(DeviceNameProperty);
  165. }
  166. set
  167. {
  168. if (!string.IsNullOrEmpty(value) && !value.StartsWith("_"))
  169. value = "_" + value;
  170. this.SetValue(DeviceNameProperty, value);
  171. }
  172. }
  173. public string DeviceId
  174. {
  175. get
  176. {
  177. return (string)this.GetValue(DeviceIdProperty);
  178. }
  179. set
  180. {
  181. this.SetValue(DeviceIdProperty, value);
  182. }
  183. }
  184. public double MaxValue
  185. {
  186. get
  187. {
  188. return (double)this.GetValue(MaxValueProperty);
  189. }
  190. set
  191. {
  192. this.SetValue(MaxValueProperty, value);
  193. // validationRule.MaxInput = value;
  194. }
  195. }
  196. public double DefaultValue
  197. {
  198. get
  199. {
  200. return (double)this.GetValue(DefaultValueProperty);
  201. }
  202. set
  203. {
  204. this.SetValue(DefaultValueProperty, value);
  205. }
  206. }
  207. public string RealValue
  208. {
  209. get
  210. {
  211. return (string)this.GetValue(RealValueProperty);
  212. }
  213. set
  214. {
  215. this.SetValue(RealValueProperty, value);
  216. }
  217. }
  218. public string Unit
  219. {
  220. get
  221. {
  222. return (string)this.GetValue(UnitProperty);
  223. }
  224. set
  225. {
  226. this.SetValue(UnitProperty, value);
  227. }
  228. }
  229. public double SetPoint
  230. {
  231. get
  232. {
  233. return (double)this.GetValue(SetPointProperty);
  234. }
  235. set
  236. {
  237. this.SetValue(SetPointProperty, value);
  238. }
  239. }
  240. public bool IsHeaterOn
  241. {
  242. get
  243. {
  244. return (bool)this.GetValue(IsHeaterOnProperty);
  245. }
  246. set
  247. {
  248. this.SetValue(IsHeaterOnProperty, value);
  249. btnHeaterOff.IsEnabled = value;
  250. btnHeaterOn.IsEnabled = !value;
  251. }
  252. }
  253. public bool EnableTemperatureSetting
  254. {
  255. get;
  256. set;
  257. }
  258. public bool EnablePowerControl
  259. {
  260. get;
  261. set;
  262. }
  263. public Action<bool> SetHeaterPowerOnOffCommandDelegate;
  264. public Action<double> SetHeaterValueCommandDelegate;
  265. public AITHeaterInputDialogBox()
  266. {
  267. InitializeComponent();
  268. DataContext = this;
  269. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  270. this.Loaded += AITHeaterInputDialogBox_Loaded;
  271. }
  272. private void AITHeaterInputDialogBox_Loaded(object sender, RoutedEventArgs e)
  273. {
  274. btnHeaterOn.Visibility = EnablePowerControl ? Visibility.Visible : Visibility.Hidden;
  275. btnHeaterOff.Visibility = EnablePowerControl ? Visibility.Visible : Visibility.Hidden;
  276. btnSet.IsEnabled = EnableTemperatureSetting;
  277. inputBox.IsEnabled = EnableTemperatureSetting;
  278. }
  279. public void FocasAll()
  280. {
  281. inputBox.Text = Math.Round(SetPoint, 2).ToString();
  282. inputBox.Focus();
  283. inputBox.SelectAll();
  284. }
  285. /// <summary>
  286. /// Vilidate input range
  287. /// </summary>
  288. /// <param name="sender"></param>
  289. /// <param name="e"></param>
  290. private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
  291. {
  292. double input;
  293. if (!double.TryParse(inputBox.Text, out input))
  294. btnSet.IsEnabled = false;
  295. else if (input < 0 || input > MaxValue)
  296. btnSet.IsEnabled = false;
  297. else
  298. btnSet.IsEnabled = true;
  299. inputBox.Foreground = btnSet.IsEnabled ?
  300. System.Windows.Media.Brushes.Black : System.Windows.Media.Brushes.Red;
  301. }
  302. private void ButtonSet_Click(object sender, RoutedEventArgs e)
  303. {
  304. try
  305. {
  306. SetHeaterValueCommandDelegate(Convert.ToDouble(inputBox.Text));
  307. //Close();
  308. }
  309. catch (Exception ex)
  310. {
  311. LOG.Error(ex.Message);
  312. }
  313. }
  314. private void OnEnterKeyIsHit(object sender, KeyEventArgs e)
  315. {
  316. try
  317. {
  318. if (!btnSet.IsEnabled)
  319. return;
  320. if (e.Key == Key.Return)
  321. {
  322. ButtonSet_Click(null, null);
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. LOG.Error(ex.Message);
  328. }
  329. }
  330. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  331. {
  332. Close();
  333. }
  334. private void ButtonHeaterOn_Click(object sender, RoutedEventArgs e)
  335. {
  336. try
  337. {
  338. if (SetHeaterPowerOnOffCommandDelegate != null)
  339. {
  340. SetHeaterPowerOnOffCommandDelegate(true);
  341. }
  342. }
  343. catch (Exception ex)
  344. {
  345. LOG.Error(ex.Message);
  346. }
  347. }
  348. private void ButtonHeaterOff_Click(object sender, RoutedEventArgs e)
  349. {
  350. try
  351. {
  352. if (SetHeaterPowerOnOffCommandDelegate != null)
  353. {
  354. SetHeaterPowerOnOffCommandDelegate(false);
  355. }
  356. }
  357. catch (Exception ex)
  358. {
  359. LOG.Error(ex.Message);
  360. }
  361. }
  362. }
  363. }