InputDialogBox.xaml.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.Shapes;
  13. using System.Globalization;
  14. using Aitex.Core.RT.Log;
  15. namespace Aitex.Core.UI.Control
  16. {
  17. public class DoubleConverter : IValueConverter
  18. {
  19. public DoubleConverter()
  20. {
  21. }
  22. // Summary:
  23. // Converts a value.
  24. //
  25. // Parameters:
  26. // value:
  27. // The value produced by the binding source.
  28. //
  29. // targetType:
  30. // The type of the binding target property.
  31. //
  32. // parameter:
  33. // The converter parameter to use.
  34. //
  35. // culture:
  36. // The culture to use in the converter.
  37. //
  38. // Returns:
  39. // A converted value. If the method returns null, the valid null value is used.
  40. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  41. {
  42. return value.ToString();
  43. }
  44. //
  45. // Summary:
  46. // Converts a value.
  47. //
  48. // Parameters:
  49. // value:
  50. // The value that is produced by the binding target.
  51. //
  52. // targetType:
  53. // The type to convert to.
  54. //
  55. // parameter:
  56. // The converter parameter to use.
  57. //
  58. // culture:
  59. // The culture to use in the converter.
  60. //
  61. // Returns:
  62. // A converted value. If the method returns null, the valid null value is used.
  63. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  64. {
  65. string input = (string)value;
  66. double result = 0;
  67. if (double.TryParse(input, out result))
  68. {
  69. return result;
  70. }
  71. return DependencyProperty.UnsetValue;
  72. }
  73. }
  74. public class InputDialogValidationRule : ValidationRule
  75. {
  76. double minInput;
  77. double maxInput;
  78. public double MinInput
  79. {
  80. get { return this.minInput; }
  81. set { this.minInput = value; }
  82. }
  83. public double MaxInput
  84. {
  85. get { return this.maxInput; }
  86. set { this.maxInput = value; }
  87. }
  88. public InputDialogValidationRule()
  89. {
  90. }
  91. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  92. {
  93. double result;
  94. // Is a number?
  95. if (!double.TryParse((string)value, out result))
  96. {
  97. string msg = string.Format("‘{0}’不是一个数.", value);
  98. return new ValidationResult(false, msg);
  99. }
  100. // Is in range?
  101. if ((result < this.minInput) || (result > this.maxInput))
  102. {
  103. string msg = string.Format("输入值必须在‘{0}’与‘{1}’之间.", this.minInput, this.maxInput);
  104. return new ValidationResult(false, msg);
  105. }
  106. // Number is valid
  107. return new ValidationResult(true, null);
  108. }
  109. }
  110. /// <summary>
  111. /// Interaction logic for InputDialogBox.xaml
  112. /// </summary>
  113. public partial class InputDialogBox : Window
  114. {
  115. public InputDialogBox()
  116. {
  117. InitializeComponent();
  118. DataContext = this;
  119. WindowStartupLocation = WindowStartupLocation.CenterOwner;
  120. }
  121. public void FocasAll()
  122. {
  123. inputBox.Text = Math.Round(SetPoint, 2).ToString();
  124. inputBox.Focus();
  125. inputBox.SelectAll();
  126. }
  127. /// <summary>
  128. /// Vilidate input range
  129. /// </summary>
  130. /// <param name="sender"></param>
  131. /// <param name="e"></param>
  132. private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
  133. {
  134. double input;
  135. if (!double.TryParse(inputBox.Text, out input)) buttonSet.IsEnabled = false;
  136. else if (input < 0 || input > MaxValue) buttonSet.IsEnabled = false;
  137. else buttonSet.IsEnabled = true;
  138. inputBox.Foreground = buttonSet.IsEnabled ?
  139. System.Windows.Media.Brushes.Black : System.Windows.Media.Brushes.Red;
  140. }
  141. public static readonly DependencyProperty DeviceNameProperty = DependencyProperty.Register(
  142. "DeviceName", typeof(string), typeof(InputDialogBox),
  143. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  144. public static readonly DependencyProperty DeviceIdProperty = DependencyProperty.Register(
  145. "DeviceId", typeof(string), typeof(InputDialogBox),
  146. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  147. public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register(
  148. "MaxValue", typeof(double), typeof(InputDialogBox),
  149. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  150. public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
  151. "DefaultValue", typeof(double), typeof(InputDialogBox),
  152. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  153. public static readonly DependencyProperty RealValueProperty = DependencyProperty.Register(
  154. "RealValue", typeof(string), typeof(InputDialogBox),
  155. new FrameworkPropertyMetadata("0.0", FrameworkPropertyMetadataOptions.AffectsRender));
  156. public static readonly DependencyProperty UnitProperty = DependencyProperty.Register(
  157. "Unit", typeof(string), typeof(InputDialogBox),
  158. new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  159. public static readonly DependencyProperty SetPointProperty = DependencyProperty.Register(
  160. "SetPoint", typeof(double), typeof(InputDialogBox),
  161. new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
  162. /// <summary>
  163. /// 是否百分比显示
  164. /// </summary>
  165. public bool IsPercent { get; set; }
  166. public string DeviceName
  167. {
  168. get
  169. {
  170. return (string)this.GetValue(DeviceNameProperty);
  171. }
  172. set
  173. {
  174. if (!string.IsNullOrEmpty(value) && !value.StartsWith("_"))
  175. value = "_" + value;
  176. this.SetValue(DeviceNameProperty, value);
  177. }
  178. }
  179. public string DeviceTitle
  180. {
  181. get
  182. {
  183. return string.Format("{0} {1}", Application.Current.Resources["GlobalLableSetPointTitle"], DeviceId);
  184. }
  185. }
  186. public string DeviceId
  187. {
  188. get
  189. {
  190. return (string)this.GetValue(DeviceIdProperty);
  191. }
  192. set
  193. {
  194. this.SetValue(DeviceIdProperty, value);
  195. }
  196. }
  197. public double MaxValue
  198. {
  199. get
  200. {
  201. return (double)this.GetValue(MaxValueProperty);
  202. }
  203. set
  204. {
  205. this.SetValue(MaxValueProperty, value);
  206. // validationRule.MaxInput = value;
  207. }
  208. }
  209. public double DefaultValue
  210. {
  211. get
  212. {
  213. return (double)this.GetValue(DefaultValueProperty);
  214. }
  215. set
  216. {
  217. this.SetValue(DefaultValueProperty, value);
  218. }
  219. }
  220. public string RealValue
  221. {
  222. get
  223. {
  224. return (string)this.GetValue(RealValueProperty);
  225. }
  226. set
  227. {
  228. this.SetValue(RealValueProperty, value);
  229. }
  230. }
  231. public string Unit
  232. {
  233. get
  234. {
  235. return (string)this.GetValue(UnitProperty);
  236. }
  237. set
  238. {
  239. this.SetValue(UnitProperty, value);
  240. }
  241. }
  242. public double SetPoint
  243. {
  244. get
  245. {
  246. return (double)this.GetValue(SetPointProperty);
  247. }
  248. set
  249. {
  250. this.SetValue(SetPointProperty, value);
  251. }
  252. }
  253. public Action<double> CommandDelegate;
  254. private void ButtonSet_Click(object sender, RoutedEventArgs e)
  255. {
  256. try
  257. {
  258. if (CommandDelegate != null)
  259. {
  260. double setp = Convert.ToDouble(inputBox.Text);
  261. if (IsPercent)
  262. setp = setp / 100.0;
  263. CommandDelegate(setp);
  264. }
  265. //Close();
  266. }
  267. catch (Exception ex)
  268. {
  269. LOG.Error(ex.Message);
  270. }
  271. }
  272. private void OnEnterKeyIsHit(object sender, KeyEventArgs e)
  273. {
  274. try
  275. {
  276. if (!buttonSet.IsEnabled) return;
  277. if (e.Key == Key.Return)
  278. {
  279. ButtonSet_Click(null, null);
  280. }
  281. }
  282. catch (Exception ex)
  283. {
  284. LOG.Error(ex.Message);
  285. }
  286. }
  287. private void ButtonCancel_Click(object sender, RoutedEventArgs e)
  288. {
  289. Close();
  290. }
  291. }
  292. }