PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/FAFramework/Forms/UtilityForm/PageDebugViewState.xaml.cs

https://github.com/11firecrackers/VT5061
C# | 260 lines | 225 code | 32 blank | 3 comment | 37 complexity | 9860e470d15bc7e08a86de6deee1a221 MD5 | raw file
  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.Xml.Linq;
  15. namespace FAFramework.Forms.UtilityForm
  16. {
  17. /// <summary>
  18. /// PageDebugViewState.xaml에 대한 상호 작용 논리
  19. /// </summary>
  20. public partial class PageDebugViewState : Page
  21. {
  22. public PageDebugViewState()
  23. {
  24. InitializeComponent();
  25. }
  26. private void buttonLoadState_Click(object sender, RoutedEventArgs e)
  27. {
  28. LoadState();
  29. }
  30. public void LoadState()
  31. {
  32. try
  33. {
  34. string path = AppDomain.CurrentDomain.BaseDirectory
  35. + @"ErrorReport\";
  36. Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
  37. dlg.InitialDirectory = path;
  38. if ((bool)dlg.ShowDialog() == false) return;
  39. XElement xel = XElement.Load(dlg.FileName);
  40. TreeViewItem tvItem = new TreeViewItem();
  41. tvItem.Header = dlg.SafeFileName;
  42. LoadTreeView(tvItem, xel);
  43. treeView1.Items.Add(tvItem);
  44. }
  45. catch (Exception e)
  46. {
  47. Manager.LogManager.Instance.WriteSystemLog(e.ToString());
  48. }
  49. }
  50. private void LoadTreeView(TreeViewItem tvItem, XElement xel)
  51. {
  52. foreach (XElement item in xel.Elements())
  53. {
  54. string type = "";
  55. if (item.Element("Type") == null)
  56. continue;
  57. else
  58. type = item.Element("Type").Value;
  59. if (type == "TreeViewItem")
  60. {
  61. LoadTreeViewItem(tvItem, item);
  62. }
  63. else if (type == "Grid")
  64. {
  65. LoadGrid(tvItem, item);
  66. }
  67. else if (type == "ListView")
  68. {
  69. tvItem.Items.Add(LoadListView(item));
  70. }
  71. }
  72. }
  73. private void LoadTreeViewItem(TreeViewItem tvItem, XElement xel)
  74. {
  75. if (xel.Element("Value") != null)
  76. {
  77. TreeViewItem subItem = new TreeViewItem();
  78. if (xel.Element("Header") != null)
  79. subItem.Header = xel.Element("Header").Value;
  80. LoadTreeView(subItem, xel.Element("Value"));
  81. tvItem.Items.Add(subItem);
  82. }
  83. }
  84. private void LoadGrid(TreeViewItem tvItem, XElement xel)
  85. {
  86. Grid grid = new Grid();
  87. grid.Margin = new System.Windows.Thickness(0);
  88. foreach (XElement item in xel.Element("Value").Elements())
  89. {
  90. string type = "";
  91. if (item.Element("Type") == null)
  92. continue;
  93. else
  94. type = item.Element("Type").Value;
  95. if (type == "TreeViewItem")
  96. {
  97. LoadTreeViewItem(tvItem, item);
  98. }
  99. else if (type == "Label")
  100. {
  101. AddChildrenToGrid(grid, LoadLabel(item));
  102. }
  103. else if (type == "CheckBox")
  104. {
  105. AddChildrenToGrid(grid, LoadCheckBox(item));
  106. }
  107. else if (type == "TextBox")
  108. {
  109. AddChildrenToGrid(grid, LoadTextBox(item));
  110. }
  111. else if (type == "ListView")
  112. {
  113. AddChildrenToGrid(grid, LoadListView(item));
  114. }
  115. }
  116. tvItem.Items.Add(grid);
  117. }
  118. private Label LoadLabel(XElement xel)
  119. {
  120. Label label = new Label();
  121. label.Padding = new System.Windows.Thickness(0);
  122. label.Margin = new System.Windows.Thickness(0);
  123. try
  124. {
  125. label.Content = xel.Element("Value").Value;
  126. }
  127. catch
  128. {
  129. }
  130. return label;
  131. }
  132. private CheckBox LoadCheckBox(XElement xel)
  133. {
  134. CheckBox checkBox = new CheckBox();
  135. checkBox.Padding = new System.Windows.Thickness(0);
  136. checkBox.Margin = new System.Windows.Thickness(0);
  137. checkBox.VerticalAlignment = System.Windows.VerticalAlignment.Center;
  138. try
  139. {
  140. bool result = false;
  141. if (bool.TryParse(xel.Element("Value").Value, out result) == false)
  142. checkBox.IsChecked = false;
  143. checkBox.IsChecked = result;
  144. }
  145. catch
  146. {
  147. }
  148. return checkBox;
  149. }
  150. private TextBox LoadTextBox(XElement xel)
  151. {
  152. TextBox textBox = new TextBox();
  153. textBox.Padding = new System.Windows.Thickness(0);
  154. textBox.Margin = new System.Windows.Thickness(0);
  155. try
  156. {
  157. textBox.Text = xel.Element("Value").Value;
  158. }
  159. catch
  160. {
  161. }
  162. return textBox;
  163. }
  164. private ListView LoadListView(XElement xel)
  165. {
  166. List<object> list = new List<object>();
  167. foreach (XElement item in xel.Element("Value").Elements())
  168. {
  169. object obj = LoadListViewItem(item);
  170. list.Add(obj);
  171. }
  172. ListView listView = new ListView();
  173. if (list.Count > 0)
  174. listView.View = GetGridView((List<dynamic>)list[0]);
  175. listView.ItemsSource = list;
  176. return listView;
  177. }
  178. private List<dynamic> LoadListViewItem(XElement xel)
  179. {
  180. List<dynamic> list = new List<dynamic>();
  181. foreach (XElement item in xel.Elements())
  182. {
  183. list.Add(new { Type = item.Element("Type").Value, Name = item.Element("Name").Value, Value = item.Element("Value").Value });
  184. }
  185. return list;
  186. }
  187. private GridView GetGridView(List<dynamic> list)
  188. {
  189. GridView gridView = new GridView();
  190. int i = 0;
  191. foreach (dynamic item in list)
  192. {
  193. GridViewColumn col = new GridViewColumn();
  194. col.Header = item.Name;
  195. if (item.Type == "System.Boolean")
  196. {
  197. System.Windows.DataTemplate template = new System.Windows.DataTemplate();
  198. System.Windows.FrameworkElementFactory checkBox =
  199. new System.Windows.FrameworkElementFactory(typeof(CheckBox));
  200. checkBox.SetValue(CheckBox.VerticalAlignmentProperty, System.Windows.VerticalAlignment.Center);
  201. Binding bd = new Binding("[" + i.ToString() + "].Value");
  202. bd.Mode = BindingMode.OneTime;
  203. checkBox.SetBinding(CheckBox.IsCheckedProperty, bd);
  204. template.VisualTree = checkBox;
  205. col.CellTemplate = template;
  206. }
  207. else
  208. {
  209. col.DisplayMemberBinding = new System.Windows.Data.Binding("[" + i.ToString() + "].Value");
  210. }
  211. gridView.Columns.Add(col);
  212. i++;
  213. }
  214. return gridView;
  215. }
  216. private void AddChildrenToGrid(Grid grid, UIElement obj)
  217. {
  218. ColumnDefinition col = new ColumnDefinition();
  219. grid.ColumnDefinitions.Add(col);
  220. grid.Children.Add(obj);
  221. Grid.SetColumn(obj, grid.Children.Count - 1);
  222. }
  223. private void buttonClear_Click(object sender, RoutedEventArgs e)
  224. {
  225. treeView1.Items.Clear();
  226. }
  227. }
  228. }