DynamicTreeView.cs 16 KB

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