AITHeaterInputDialogBox.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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)
  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 DefaultValueProperty = DependencyProperty.Register(
  167. "DefaultValue", typeof(double), typeof(AITHeaterInputDialogBox),
  168. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  169. public static readonly DependencyProperty RealValueProperty = DependencyProperty.Register(
  170. "RealValue", typeof(string), typeof(AITHeaterInputDialogBox),
  171. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  172. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
  173. "Unit", typeof(string), typeof(AITHeaterInputDialogBox),
  174. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  175. public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
  176. "SetPoint", typeof(double), typeof(AITHeaterInputDialogBox),
  177. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  178. public static readonly DependencyProperty IsHeaterOnProperty = DependencyProperty.Register(
  179. "IsHeaterOn", typeof(bool), typeof(AITHeaterInputDialogBox),
  180. new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  181. /// <summary>
  182. /// 是否百分比显示
  183. /// </summary>
  184. public bool IsPercent
  185. {
  186. get;
  187. set;
  188. }
  189. public string DeviceName
  190. {
  191. get
  192. {
  193. return (string)this.GetValue(DeviceNameProperty);
  194. }
  195. set
  196. {
  197. if (!string.IsNullOrEmpty(value) && !value.StartsWith("_"))
  198. value = "_" + value;
  199. this.SetValue(DeviceNameProperty, value);
  200. }
  201. }
  202. public string DeviceId
  203. {
  204. get
  205. {
  206. return (string)this.GetValue(DeviceIdProperty);
  207. }
  208. set
  209. {
  210. this.SetValue(DeviceIdProperty, value);
  211. }
  212. }
  213. public double MaxValue
  214. {
  215. get
  216. {
  217. return (double)this.GetValue(MaxValueProperty);
  218. }
  219. set
  220. {
  221. this.SetValue(MaxValueProperty, value);
  222. // validationRule.MaxInput = value;
  223. }
  224. }
  225. public double DefaultValue
  226. {
  227. get
  228. {
  229. return (double)this.GetValue(DefaultValueProperty);
  230. }
  231. set
  232. {
  233. this.SetValue(DefaultValueProperty, value);
  234. }
  235. }
  236. public string RealValue
  237. {
  238. get
  239. {
  240. return (string)this.GetValue(RealValueProperty);
  241. }
  242. set
  243. {
  244. this.SetValue(RealValueProperty, value);
  245. }
  246. }
  247. public string Unit
  248. {
  249. get
  250. {
  251. return (string)this.GetValue(UnitProperty);
  252. }
  253. set
  254. {
  255. this.SetValue(UnitProperty, value);
  256. }
  257. }
  258. public double SetPoint
  259. {
  260. get
  261. {
  262. return (double)this.GetValue(SetPointProperty);
  263. }
  264. set
  265. {
  266. this.SetValue(SetPointProperty, value);
  267. }
  268. }
  269. public bool IsHeaterOn
  270. {
  271. get
  272. {
  273. return (bool)this.GetValue(IsHeaterOnProperty);
  274. }
  275. set
  276. {
  277. this.SetValue(IsHeaterOnProperty, value);
  278. btnHeaterOff.IsEnabled = value;
  279. btnHeaterOn.IsEnabled = !value;
  280. }
  281. }
  282. public Action<bool> SetHeaterPowerOnOffCommandDelegate;
  283. public Action<double> SetHeaterValueCommandDelegate;
  284. private void ButtonSet_Click(object sender, RoutedEventArgs e)
  285. {
  286. try
  287. {
  288. SetHeaterValueCommandDelegate(Convert.ToDouble(inputBox.Text));
  289. Close();
  290. }
  291. catch (Exception ex)
  292. {
  293. LOG.Error(ex.Message);
  294. }
  295. }
  296. private void OnEnterKeyIsHit(object sender, KeyEventArgs e)
  297. {
  298. try
  299. {
  300. if (!btnSet.IsEnabled)
  301. return;
  302. if (e.Key == Key.Return)
  303. {
  304. ButtonSet_Click(null, null);
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. LOG.Error(ex.Message);
  310. }
  311. }
  312. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  313. {
  314. Close();
  315. }
  316. private void ButtonHeaterOn_Click(object sender, RoutedEventArgs e)
  317. {
  318. try
  319. {
  320. if (SetHeaterPowerOnOffCommandDelegate != null)
  321. {
  322. SetHeaterPowerOnOffCommandDelegate(true);
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. LOG.Error(ex.Message);
  328. }
  329. }
  330. private void ButtonHeaterOff_Click(object sender, RoutedEventArgs e)
  331. {
  332. try
  333. {
  334. if (SetHeaterPowerOnOffCommandDelegate != null)
  335. {
  336. SetHeaterPowerOnOffCommandDelegate(false);
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. LOG.Error(ex.Message);
  342. }
  343. }
  344. }
  345. }