DynamicTreeView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 abstract class DynamicTreeView<T> : ContentControl where T : BaseObject, new()
  117. {
  118. protected abstract Expression<Func<T, Guid>> ID { get; }
  119. protected abstract Expression<Func<T, Guid>> ParentID { get; }
  120. protected abstract Expression<Func<T, String>> Description { get; }
  121. protected CoreTable Data { get; private set; }
  122. private ContextMenu _menu = null;
  123. private SfTreeGrid _tree = null;
  124. private DockPanel _dock = null;
  125. private Grid _grid = null;
  126. private Button _add = null;
  127. private Button _edit = null;
  128. private Button _delete = null;
  129. private Label _spacer = null;
  130. public FluentList<DynamicTreeOption> Options { get; private set; }
  131. public event OnSelectItem OnSelectItem;
  132. private double minRowHeight = 30D;
  133. private double maxRowHeight = 30D;
  134. public double MinRowHeight
  135. {
  136. get => minRowHeight;
  137. set
  138. {
  139. minRowHeight = value;
  140. CalculateRowHeight();
  141. }
  142. }
  143. public double MaxRowHeight
  144. {
  145. get => maxRowHeight;
  146. set
  147. {
  148. maxRowHeight = value;
  149. CalculateRowHeight();
  150. }
  151. }
  152. /*public double RowHeight
  153. {
  154. get => _tree.RowHeight;
  155. set => _tree.RowHeight = value;
  156. }*/
  157. public DynamicTreeView() : base()
  158. {
  159. Options = new FluentList<DynamicTreeOption>();
  160. Options.OnChanged += OptionsChanged;
  161. _grid = new Grid();
  162. _grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1D, GridUnitType.Star) });
  163. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Star) });
  164. _grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1D, GridUnitType.Auto) });
  165. _menu = new ContextMenu();
  166. var additem = new MenuItem() { Header = "Add Child Folder" };
  167. additem.Click += (o, e) => { AddItem((_tree.SelectedItem as DynamicTreeNode).ID); };
  168. _menu.Items.Add(additem);
  169. _tree = new SfTreeGrid();
  170. _tree.ChildPropertyName = "Children";
  171. //_tree.ParentPropertyName = "Parent";
  172. _tree.AutoGenerateColumns = false;
  173. _tree.AutoExpandMode = AutoExpandMode.AllNodesExpanded;
  174. //_tree.NodeCollapsing += (o, e) => { e.Cancel = true; };
  175. _tree.HeaderRowHeight = 0D;
  176. _tree.SelectionChanged += (o,e) => OnSelectItem?.Invoke(_tree.SelectedItem as DynamicTreeNode);
  177. _tree.AllowSelectionOnExpanderClick = false;
  178. _tree.ContextMenu = _menu;
  179. _tree.Background = new SolidColorBrush(Colors.DimGray);
  180. var cellStyle = new Style(typeof(TreeGridRowControl));
  181. cellStyle.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(Colors.White)));
  182. _tree.RowStyle = cellStyle;
  183. _tree.SelectionBackground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0x11, 0x9E, 0xD9));
  184. _tree.Columns.Add(new TreeGridTextColumn()
  185. {
  186. MappingName = "Description"
  187. }
  188. );
  189. _tree.ColumnSizer = TreeColumnSizer.Star;
  190. _tree.RowHeight = 30D;
  191. _tree.SetValue(Grid.RowProperty, 0);
  192. _grid.Children.Add(_tree);
  193. _dock = new DockPanel();
  194. _dock.SetValue(Grid.RowProperty, 1);
  195. _grid.Children.Add(_dock);
  196. _add = CreateButton(Properties.Resources.add.AsBitmapImage(System.Drawing.Color.White), "", "Add Item", (o) => AddItem(Guid.Empty));
  197. _add.Margin = new Thickness(0, 2, 2, 0);
  198. _add.Visibility = Visibility.Collapsed;
  199. _add.SetValue(DockPanel.DockProperty, Dock.Left);
  200. _dock.Children.Add(_add);
  201. _edit = CreateButton(Properties.Resources.pencil.AsBitmapImage(System.Drawing.Color.White), "", "Edit Item", EditItem);
  202. _edit.Margin = new Thickness(0, 2, 2, 0);
  203. _edit.Visibility = Visibility.Collapsed;
  204. _edit.SetValue(DockPanel.DockProperty, Dock.Left);
  205. _dock.Children.Add(_edit);
  206. _delete = CreateButton(Properties.Resources.delete.AsBitmapImage(System.Drawing.Color.White), "", "Delete Item", DeleteItem);
  207. _delete.Margin = new Thickness(2, 2, 0, 0);
  208. _delete.Visibility = Visibility.Collapsed;
  209. _delete.SetValue(DockPanel.DockProperty, Dock.Right);
  210. _dock.Children.Add(_delete);
  211. _spacer = new Label();
  212. _spacer.SetValue(DockPanel.DockProperty, Dock.Left);
  213. _dock.Children.Add(_spacer);
  214. Content = _grid;
  215. SizeChanged += DynamicTreeView_SizeChanged;
  216. }
  217. private void DynamicTreeView_SizeChanged(object sender, SizeChangedEventArgs e)
  218. {
  219. CalculateRowHeight();
  220. }
  221. private void CalculateRowHeight()
  222. {
  223. if(Data != null && Data.Rows.Count > 0)
  224. {
  225. var contentHeight = _tree.ActualHeight - (_tree.Padding.Top + _tree.Padding.Bottom) - 2; // Two extra pixels of space
  226. var targetHeight = contentHeight / Data.Rows.Count;
  227. _tree.RowHeight = Math.Max(Math.Min(targetHeight, MaxRowHeight), MinRowHeight);
  228. }
  229. }
  230. private Button CreateButton(BitmapImage? image = null, string? text = null, string? tooltip = null, Action<Button>? action = null)
  231. {
  232. var button = new Button();
  233. button.SetValue(BorderBrushProperty, new SolidColorBrush(Colors.Gray));
  234. button.SetValue(BorderThicknessProperty, new Thickness(0.75));
  235. button.Height = 30;
  236. button.MinWidth = 30;
  237. button.Click += (o, e) => action?.Invoke(button);
  238. UpdateButton(button, image, text, tooltip);
  239. return button;
  240. }
  241. protected void UpdateButton(Button button, BitmapImage? image, string? text, string? tooltip = null)
  242. {
  243. var stackPnl = new StackPanel();
  244. stackPnl.Orientation = Orientation.Horizontal;
  245. if (image != null)
  246. {
  247. var img = new Image();
  248. img.Source = image;
  249. img.Margin = new Thickness(2);
  250. img.ToolTip = tooltip;
  251. stackPnl.Children.Add(img);
  252. }
  253. if (!string.IsNullOrEmpty(text))
  254. {
  255. var lbl = new Label();
  256. lbl.Content = text;
  257. lbl.VerticalAlignment = VerticalAlignment.Stretch;
  258. lbl.VerticalContentAlignment = VerticalAlignment.Center;
  259. lbl.Margin = new Thickness(2, 0, 5, 0);
  260. lbl.ToolTip = ToolTip;
  261. stackPnl.Children.Add(lbl);
  262. }
  263. button.Content = stackPnl;
  264. button.ToolTip = tooltip;
  265. }
  266. private void OptionsChanged(object sender, EventArgs args)
  267. {
  268. _add.Visibility = Options.Contains(DynamicTreeOption.Add) ? Visibility.Visible : Visibility.Collapsed;
  269. _edit.Visibility = Options.Contains(DynamicTreeOption.Edit) ? Visibility.Visible : Visibility.Collapsed;
  270. _delete.Visibility = Options.Contains(DynamicTreeOption.Delete) ? Visibility.Visible : Visibility.Collapsed;
  271. }
  272. protected virtual T DoCreateItem(Guid parent)
  273. {
  274. T result = new T();
  275. var node = _tree.SelectedItem as DynamicTreeNode;
  276. CoreUtils.SetPropertyValue(result, CoreUtils.GetFullPropertyName(ParentID, "."), parent);
  277. return result;
  278. }
  279. protected abstract T? DoLoadItem(Guid id);
  280. protected virtual bool DoEditItem(T item)
  281. {
  282. var form = new DynamicEditorForm(typeof(T));
  283. form.Items = new T[] { item };
  284. return form.ShowDialog() == true;
  285. }
  286. protected abstract void DoSaveItem(T item);
  287. protected abstract bool DoDeleteItem(Guid id);
  288. private void AddItem(Guid id)
  289. {
  290. try
  291. {
  292. T item = DoCreateItem(id);
  293. if (DoEditItem(item))
  294. {
  295. DoSaveItem(item);
  296. Refresh();
  297. }
  298. }
  299. catch (Exception e)
  300. {
  301. MessageBox.Show(e.Message);
  302. }
  303. }
  304. private void EditItem(Button button)
  305. {
  306. var node = _tree.SelectedItem as DynamicTreeNode;
  307. if (node == null)
  308. {
  309. MessageBox.Show("Please Select an item to edit!");
  310. return;
  311. }
  312. var item = DoLoadItem(node.ID);
  313. if (item != null && DoEditItem(item))
  314. {
  315. DoSaveItem(item);
  316. Refresh();
  317. }
  318. }
  319. private void DeleteItem(Button button)
  320. {
  321. var node = _tree.SelectedItem as DynamicTreeNode;
  322. if (node == null)
  323. {
  324. MessageBox.Show("Please Select an item to edit!");
  325. return;
  326. }
  327. if (DoDeleteItem(node.ID))
  328. {
  329. Refresh();
  330. }
  331. }
  332. public DynamicTreeNodes Nodes { get; set; }
  333. protected abstract void DoRefresh(Action<CoreTable?, Exception?> action);
  334. private void AfterRefresh()
  335. {
  336. var nodes = new DynamicTreeNodes();
  337. foreach (var row in Data.Rows)
  338. {
  339. var _id = row.Get(ID);
  340. var _parent = row.Get(ParentID);
  341. var _description = row.Get(Description);
  342. nodes.Add(_id, _parent).Description = _description;
  343. }
  344. Nodes = nodes;
  345. _tree.ItemsSource = nodes.Nodes;
  346. CalculateRowHeight();
  347. }
  348. public void Refresh()
  349. {
  350. DoRefresh((table, exception) =>
  351. {
  352. if(exception != null)
  353. {
  354. Dispatcher.Invoke(() =>
  355. {
  356. MessageBox.Show(String.Format("Error: {0}", exception.Message));
  357. });
  358. }
  359. Data = table;
  360. Dispatcher.Invoke(() =>
  361. {
  362. AfterRefresh();
  363. });
  364. });
  365. }
  366. }
  367. }