ParameterView.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 Aitex.Core.RT.Log;
  15. using System.Xml;
  16. using Aitex.Core.RT.ConfigCenter;
  17. using Aitex.Core.UI.View.Frame;
  18. namespace Aitex.Core.UI.View.Common
  19. {
  20. /// <summary>
  21. /// Interaction logic for ParameterView.xaml
  22. /// </summary>
  23. public partial class ParameterView : UserControl
  24. {
  25. bool _isLoaded;
  26. ParameterViewModel _vm;
  27. string _gridEditorOldValue;
  28. bool _isGridModified;
  29. public ParameterView()
  30. {
  31. InitializeComponent();
  32. this.Loaded += new RoutedEventHandler(ParameterView_Loaded);
  33. }
  34. void ParameterView_Loaded(object sender, RoutedEventArgs e)
  35. {
  36. //set permission
  37. this.IsEnabled = true;
  38. this.Name = "Parameter";
  39. Account.ViewPermission viewPermission; // chaossong@add 20160414 for permission
  40. if (ViewManager.LoginAccount != null)
  41. viewPermission = ViewManager.LoginAccount.Permission[this.Name];
  42. else
  43. viewPermission = Account.ViewPermission.FullyControl;
  44. switch (viewPermission)
  45. {
  46. case Account.ViewPermission.FullyControl:
  47. btnResetParam.IsEnabled = true;
  48. btnSaveParam.IsEnabled = true;
  49. dataGrid1.IsEnabled = true;
  50. break;
  51. default:
  52. btnResetParam.IsEnabled = false;
  53. btnSaveParam.IsEnabled = false;
  54. dataGrid1.IsEnabled = false;
  55. break;
  56. }
  57. if (_isLoaded)
  58. return;
  59. _vm = DataContext as ParameterViewModel;
  60. if (_vm == null)
  61. throw new ApplicationException("系统配置页面没有设置DataContext,需要在窗口创建之前,设置初始配置数据");
  62. InitTree();
  63. _isLoaded = true;
  64. }
  65. void InitTree()
  66. {
  67. treeView1.Items.Clear();
  68. List<KeyValuePair<string, string>> lstSections = _vm.GetSectionList();
  69. foreach (KeyValuePair<string, string> item in lstSections)
  70. {
  71. var fNode = new TreeViewFolderItem(item.Key);
  72. fNode.Tag = item;
  73. fNode.Selected += new RoutedEventHandler(sectionNode_Selected);
  74. treeView1.Items.Add(fNode);
  75. }
  76. }
  77. void sectionNode_Selected(object sender, RoutedEventArgs e)
  78. {
  79. var selectedNode = sender as TreeViewFolderItem;
  80. if (selectedNode == null)
  81. return;
  82. if (_isGridModified)
  83. {
  84. if (MessageBox.Show("是否需要保存未存盘的参数?", "未保存", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
  85. {
  86. btnSaveParam_Click(null, null);
  87. }
  88. else
  89. {
  90. _isGridModified = false;
  91. }
  92. }
  93. KeyValuePair<string, string> keyPair = (KeyValuePair<string, string>)selectedNode.Tag;
  94. labelTile.Content = keyPair.Key;
  95. dataGrid1.Tag = keyPair.Value;
  96. dataGrid1.ItemsSource = _vm.GetConfigEntries(keyPair.Value);
  97. }
  98. private void GridEditor_LostFocus(object sender, RoutedEventArgs e)
  99. {
  100. TextBox tb = (sender as TextBox);
  101. if (tb != null)
  102. _isGridModified = tb.Text != _gridEditorOldValue;
  103. }
  104. private void GridEditor_GotFocus(object sender, RoutedEventArgs e)
  105. {
  106. TextBox tb = (sender as TextBox);
  107. if (tb != null)
  108. _gridEditorOldValue = tb.Text;
  109. }
  110. private void btnSaveParam_Click(object sender, RoutedEventArgs e)
  111. {
  112. _isGridModified = false;
  113. string reason;
  114. if (!VerifyInputData(out reason))
  115. {
  116. MessageBox.Show(reason, "Aitex", MessageBoxButton.OK, MessageBoxImage.Warning);
  117. return;
  118. }
  119. string xpath = dataGrid1.Tag as string;
  120. if (_vm.SaveConfigSection(xpath.Substring(xpath.IndexOf("'") + 1, xpath.LastIndexOf("'") - xpath.IndexOf("'")-1), dataGrid1.ItemsSource as List<ConfigEntry>))
  121. MessageBox.Show("设定值保存成功!", "Aitex", MessageBoxButton.OK, MessageBoxImage.Information);
  122. else
  123. MessageBox.Show("设定值保存失败!", "Aitex", MessageBoxButton.OK, MessageBoxImage.Error);
  124. btnResetParam_Click(null, null);
  125. }
  126. private void btnResetParam_Click(object sender, RoutedEventArgs e)
  127. {
  128. dataGrid1.ItemsSource = _vm.GetConfigEntries(dataGrid1.Tag as string);
  129. }
  130. private bool VerifyInputData(out string reason)
  131. {
  132. bool ret = true;
  133. dataGrid1.CancelEdit();
  134. var entryList = this.dataGrid1.ItemsSource.Cast<object>();
  135. reason = string.Empty;
  136. foreach (ConfigEntry entry in entryList)
  137. {
  138. try
  139. {
  140. var inputText = entry.Value;
  141. var typeText = entry.Type;
  142. var minText = entry.RangeLowLimit;
  143. var maxText = entry.RangeUpLimit;
  144. if (string.IsNullOrEmpty(inputText))
  145. continue;
  146. if (String.Compare(typeText, "Int32", true) == 0)
  147. {
  148. int res = 0;
  149. if (!int.TryParse(inputText, out res))
  150. throw new Exception();
  151. int min = Convert.ToInt32(minText);
  152. int max = Convert.ToInt32(maxText);
  153. if (res < min || res > max)
  154. {
  155. ret = false;
  156. reason += string.Format("{0} 输入值错误!\n", entry.Description);
  157. var textBlock = dataGrid1.Columns[7].GetCellContent(entry) as System.Windows.Controls.TextBlock;
  158. if (textBlock != null)
  159. textBlock.Background = Brushes.Red;
  160. }
  161. }
  162. else if (String.Compare(typeText, "Double", true) == 0)
  163. {
  164. double res = 0;
  165. if (!double.TryParse(inputText, out res))
  166. throw new Exception();
  167. double min = Convert.ToDouble(minText);
  168. double max = Convert.ToDouble(maxText);
  169. if (res < min || res > max)
  170. {
  171. ret = false;
  172. reason += string.Format("{0} 输入值错误!\n", entry.Description);
  173. var textBlock = dataGrid1.Columns[7].GetCellContent(entry) as System.Windows.Controls.TextBlock;
  174. if (textBlock != null)
  175. textBlock.Background = Brushes.Red;
  176. }
  177. }
  178. }
  179. catch (Exception)
  180. {
  181. reason += string.Format("{0} 输入值错误!\n", entry.Description);
  182. ret = false;
  183. }
  184. }
  185. return ret;
  186. }
  187. }
  188. }