DynamicTreeView.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Linq.Expressions;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using InABox.Core;
  12. using InABox.WPF;
  13. using Syncfusion.UI.Xaml.Grid;
  14. using Syncfusion.UI.Xaml.TreeGrid;
  15. namespace InABox.DynamicGrid
  16. {
  17. public enum DynamicTreeOption
  18. {
  19. Add,
  20. Edit,
  21. Delete
  22. }
  23. public enum DynamicTreeGridLines
  24. {
  25. Both,
  26. Horizontal,
  27. Vertical,
  28. None
  29. }
  30. public enum DynamicTreeGridExpandMode
  31. {
  32. All,
  33. Root,
  34. None
  35. }
  36. public delegate void OnSelectItem(CoreTreeNode node);
  37. public delegate void OnContextMenuOpening(CoreTreeNode node, ContextMenu menu);
  38. public abstract class DynamicTreeView<T> : ContentControl where T : BaseObject, new()
  39. {
  40. protected abstract Expression<Func<T, Guid>> ID { get; }
  41. protected abstract Expression<Func<T, Guid>> ParentID { get; }
  42. protected abstract Expression<Func<T, String>> Description { get; }
  43. public CoreTable Data { get; private set; }
  44. private ContextMenu _menu;
  45. private SfTreeGrid _tree;
  46. private DockPanel _dock;
  47. private Grid _grid;
  48. private Button _add;
  49. private Button _edit;
  50. private Button _delete;
  51. private Label _spacer;
  52. public FluentList<DynamicTreeOption> Options { get; private set; }
  53. public event OnSelectItem OnSelectItem;
  54. public event OnContextMenuOpening OnContextMenuOpening;
  55. private double minRowHeight = 30D;
  56. private double maxRowHeight = 30D;
  57. public double MinRowHeight
  58. {
  59. get => minRowHeight;
  60. set
  61. {
  62. minRowHeight = value;
  63. CalculateRowHeight();
  64. }
  65. }
  66. public double MaxRowHeight
  67. {
  68. get => maxRowHeight;
  69. set
  70. {
  71. maxRowHeight = value;
  72. CalculateRowHeight();
  73. }
  74. }
  75. private bool _shownumbers = false;
  76. public bool ShowNumbers
  77. {
  78. get => _shownumbers;
  79. set
  80. {
  81. _shownumbers = value;
  82. _tree.Columns[1].Width = value ? 50 : 0;
  83. }
  84. }
  85. private DynamicTreeGridLines _gridLines = DynamicTreeGridLines.Both;
  86. public DynamicTreeGridLines GridLines
  87. {
  88. get => _gridLines;
  89. set
  90. {
  91. _gridLines = value;
  92. _tree.GridLinesVisibility = value switch
  93. {
  94. DynamicTreeGridLines.Both => GridLinesVisibility.Both,
  95. DynamicTreeGridLines.Horizontal => GridLinesVisibility.Horizontal,
  96. DynamicTreeGridLines.Vertical => GridLinesVisibility.Vertical,
  97. _ => GridLinesVisibility.None
  98. };
  99. }
  100. }
  101. public DynamicTreeGridExpandMode ExpandMode
  102. {
  103. get
  104. {
  105. return _tree.AutoExpandMode switch
  106. {
  107. AutoExpandMode.AllNodesExpanded => DynamicTreeGridExpandMode.All,
  108. AutoExpandMode.RootNodesExpanded => DynamicTreeGridExpandMode.Root,
  109. _ => DynamicTreeGridExpandMode.None
  110. };
  111. }
  112. set
  113. {
  114. _tree.AutoExpandMode = value switch
  115. {
  116. DynamicTreeGridExpandMode.All => AutoExpandMode.AllNodesExpanded,
  117. DynamicTreeGridExpandMode.Root => AutoExpandMode.RootNodesExpanded,
  118. _ => AutoExpandMode.None
  119. };
  120. }
  121. }
  122. public Guid SelectedID => (_tree?.SelectedItem as CoreTreeNode)?.ID ?? Guid.Empty;
  123. /*public double RowHeight
  124. {
  125. get => _tree.RowHeight;
  126. set => _tree.RowHeight = value;
  127. }*/
  128. public DynamicTreeView() : base()
  129. {
  130. Options = new FluentList<DynamicTreeOption>();
  131. Options.OnChanged += OptionsChanged;
  132. _grid = new Grid();
  133. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1D, GridUnitType.Star) });
  134. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Star) });
  135. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Auto) });
  136. _tree = new SfTreeGrid();
  137. _tree.ChildPropertyName = "Children";
  138. //_tree.ParentPropertyName = "Parent";
  139. _tree.AutoGenerateColumns = false;
  140. //_tree.NodeCollapsing += (o, e) => { e.Cancel = true; };
  141. _tree.HeaderRowHeight = 0D;
  142. _tree.SelectionChanged += (o,e) => OnSelectItem?.Invoke((_tree.SelectedItem as CoreTreeNode)!);
  143. _tree.AllowSelectionOnExpanderClick = false;
  144. ExpandMode = DynamicTreeGridExpandMode.All;
  145. _menu = new ContextMenu();
  146. //var additem = new MenuItem() { Header = "Add Child Folder" };
  147. //additem.Click += (o, e) => { DoAddItem((_tree.SelectedItem as CoreTreeNode)!.ID, true); };
  148. //_menu.Items.Add(additem);
  149. _tree.ContextMenuOpening += _tree_ContextMenuOpening;
  150. _tree.ContextMenu = _menu;
  151. _tree.Background = new SolidColorBrush(Colors.DimGray);
  152. var cellStyle = new Style(typeof(TreeGridRowControl));
  153. cellStyle.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.White)));
  154. _tree.RowStyle = cellStyle;
  155. _tree.SelectionBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x11, 0x9E, 0xD9));
  156. _tree.Columns.Add(new TreeGridTextColumn()
  157. {
  158. MappingName = "Description"
  159. }
  160. );
  161. _tree.Columns.Add(new TreeGridTextColumn()
  162. {
  163. MappingName = "Number",
  164. Width = _shownumbers ? 50 : 0,
  165. TextAlignment = TextAlignment.Right
  166. }
  167. );
  168. _tree.ColumnSizer = TreeColumnSizer.Star;
  169. _tree.RowHeight = 30D;
  170. _tree.SetValue(Grid.RowProperty, 0);
  171. _grid.Children.Add(_tree);
  172. _dock = new DockPanel();
  173. _dock.SetValue(Grid.RowProperty, 1);
  174. _grid.Children.Add(_dock);
  175. _add = CreateButton(Wpf.Resources.add.AsBitmapImage(System.Drawing.Color.White), "", "Add Item", (o) => DoAddItem(Guid.Empty, true));
  176. _add.Margin = new Thickness(0, 2, 2, 0);
  177. _add.Visibility = Visibility.Collapsed;
  178. _add.SetValue(DockPanel.DockProperty, Dock.Left);
  179. _dock.Children.Add(_add);
  180. _edit = CreateButton(Wpf.Resources.pencil.AsBitmapImage(System.Drawing.Color.White), "", "Edit Item", EditItem);
  181. _edit.Margin = new Thickness(0, 2, 2, 0);
  182. _edit.Visibility = Visibility.Collapsed;
  183. _edit.SetValue(DockPanel.DockProperty, Dock.Left);
  184. _dock.Children.Add(_edit);
  185. _delete = CreateButton(Wpf.Resources.delete.AsBitmapImage(System.Drawing.Color.White), "", "Delete Item", DeleteItem);
  186. _delete.Margin = new Thickness(2, 2, 0, 0);
  187. _delete.Visibility = Visibility.Collapsed;
  188. _delete.SetValue(DockPanel.DockProperty, Dock.Right);
  189. _dock.Children.Add(_delete);
  190. _spacer = new Label();
  191. _spacer.SetValue(DockPanel.DockProperty, Dock.Left);
  192. _dock.Children.Add(_spacer);
  193. Content = _grid;
  194. SizeChanged += DynamicTreeView_SizeChanged;
  195. }
  196. #region Public Interface
  197. public void AddItem(CoreTreeNode? parentNode = null, bool edit = true)
  198. {
  199. var id = parentNode?.ID ?? Guid.Empty;
  200. DoAddItem(id, edit);
  201. }
  202. #endregion
  203. private void _tree_ContextMenuOpening(object sender, ContextMenuEventArgs e)
  204. {
  205. _menu.Items.Clear();
  206. if (OnContextMenuOpening is not null)
  207. {
  208. OnContextMenuOpening.Invoke((_tree.SelectedItem as CoreTreeNode)!, _menu);
  209. if(_menu.Items.Count == 0)
  210. {
  211. e.Handled = true;
  212. }
  213. }
  214. else
  215. {
  216. if (Options.Contains(DynamicTreeOption.Add))
  217. {
  218. _menu.AddItem("Add Item", null, (_tree.SelectedItem as CoreTreeNode)!.ID, (id) => DoAddItem(id, true));
  219. }
  220. }
  221. }
  222. private void DynamicTreeView_SizeChanged(object sender, SizeChangedEventArgs e)
  223. {
  224. CalculateRowHeight();
  225. }
  226. private void CalculateRowHeight()
  227. {
  228. if(Data != null && Data.Rows.Count > 0)
  229. {
  230. var contentHeight = _tree.ActualHeight - (_tree.Padding.Top + _tree.Padding.Bottom) - 2; // Two extra pixels of space
  231. var targetHeight = contentHeight / Data.Rows.Count;
  232. _tree.RowHeight = Math.Max(Math.Min(targetHeight, MaxRowHeight), MinRowHeight);
  233. }
  234. }
  235. private Button CreateButton(BitmapImage? image = null, string? text = null, string? tooltip = null, Action<Button>? action = null)
  236. {
  237. var button = new Button();
  238. button.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
  239. button.SetValue(BorderThicknessProperty, new Thickness(0.75));
  240. button.Height = 30;
  241. button.MinWidth = 30;
  242. button.Click += (o, e) => action?.Invoke(button);
  243. UpdateButton(button, image, text, tooltip);
  244. return button;
  245. }
  246. protected void UpdateButton(Button button, BitmapImage? image, string? text, string? tooltip = null)
  247. {
  248. var stackPnl = new StackPanel();
  249. stackPnl.Orientation = Orientation.Horizontal;
  250. if (image != null)
  251. {
  252. var img = new Image();
  253. img.Source = image;
  254. img.Margin = new Thickness(2);
  255. img.ToolTip = tooltip;
  256. stackPnl.Children.Add(img);
  257. }
  258. if (!string.IsNullOrEmpty(text))
  259. {
  260. var lbl = new Label();
  261. lbl.Content = text;
  262. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  263. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  264. lbl.Margin = new Thickness(2, 0, 5, 0);
  265. lbl.ToolTip = ToolTip;
  266. stackPnl.Children.Add(lbl);
  267. }
  268. button.Content = stackPnl;
  269. button.ToolTip = tooltip;
  270. }
  271. private void OptionsChanged(object sender, EventArgs args)
  272. {
  273. _add.Visibility = Options.Contains(DynamicTreeOption.Add) ? Visibility.Visible : Visibility.Collapsed;
  274. _edit.Visibility = Options.Contains(DynamicTreeOption.Edit) ? Visibility.Visible : Visibility.Collapsed;
  275. _delete.Visibility = Options.Contains(DynamicTreeOption.Delete) ? Visibility.Visible : Visibility.Collapsed;
  276. }
  277. protected virtual T DoCreateItem(Guid parent)
  278. {
  279. T result = new T();
  280. CoreUtils.SetPropertyValue(result, CoreUtils.GetFullPropertyName(ParentID, "."), parent);
  281. return result;
  282. }
  283. protected abstract T? DoLoadItem(Guid id);
  284. protected virtual bool DoEditItem(T item)
  285. {
  286. var grid = DynamicGridUtils.CreateDynamicGrid<T>(typeof(DynamicItemsListGrid<>));
  287. return grid.EditItems(new T[] { item });
  288. }
  289. protected abstract void DoSaveItem(T item);
  290. protected abstract bool DoDeleteItem(Guid id);
  291. protected virtual void DoAddItem(Guid id, bool edit)
  292. {
  293. try
  294. {
  295. T item = DoCreateItem(id);
  296. if (edit)
  297. {
  298. if (DoEditItem(item))
  299. {
  300. DoSaveItem(item);
  301. Refresh();
  302. }
  303. }
  304. else
  305. {
  306. DoSaveItem(item);
  307. Refresh();
  308. }
  309. }
  310. catch (Exception e)
  311. {
  312. MessageBox.Show(e.Message);
  313. }
  314. }
  315. private void EditItem(Button button)
  316. {
  317. var node = _tree.SelectedItem as CoreTreeNode;
  318. if (node == null)
  319. {
  320. MessageBox.Show("Please Select an item to edit!");
  321. return;
  322. }
  323. var item = DoLoadItem(node.ID);
  324. if (item != null && DoEditItem(item))
  325. {
  326. DoSaveItem(item);
  327. Refresh();
  328. }
  329. }
  330. private void DeleteItem(Button button)
  331. {
  332. var node = _tree.SelectedItem as CoreTreeNode;
  333. if (node == null)
  334. {
  335. MessageBox.Show("Please Select an item to edit!");
  336. return;
  337. }
  338. if (DoDeleteItem(node.ID))
  339. {
  340. Refresh();
  341. }
  342. }
  343. public CoreTreeNodes Nodes { get; set; }
  344. protected abstract void DoRefresh(Action<CoreTable?, Exception?> action);
  345. private void AfterRefresh()
  346. {
  347. var nodes = new CoreTreeNodes();
  348. foreach (var row in Data.Rows)
  349. {
  350. var _id = row.Get(ID);
  351. var _parent = row.Get(ParentID);
  352. var _description = row.Get(Description);
  353. nodes.Add(_id, _parent).Description = _description;
  354. }
  355. Nodes = nodes;
  356. _tree.ItemsSource = nodes.Nodes;
  357. CalculateRowHeight();
  358. }
  359. public void Refresh()
  360. {
  361. _tree.ItemsSource = null;
  362. DoRefresh((table, exception) =>
  363. {
  364. if(exception != null)
  365. {
  366. Dispatcher.Invoke(() =>
  367. {
  368. MessageBox.Show(String.Format("Error: {0}", exception.Message));
  369. });
  370. }
  371. else if(table is not null)
  372. {
  373. Data = table;
  374. Dispatcher.Invoke(() =>
  375. {
  376. AfterRefresh();
  377. });
  378. }
  379. });
  380. }
  381. }
  382. }