DynamicGridColumnNameSelectorWindow.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using InABox.Core;
  2. using InABox.WPF;
  3. using Syncfusion.Pdf.Parsing;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Shapes;
  19. namespace InABox.DynamicGrid;
  20. public class DynamicGridColumnNameSelectorGrid : DynamicItemsListGrid<DynamicGridColumnNameSelectorItem>, INotifyPropertyChanged
  21. {
  22. private bool _canSave;
  23. public bool CanSave
  24. {
  25. get => _canSave;
  26. set
  27. {
  28. _canSave = value;
  29. DoPropertyChanged();
  30. }
  31. }
  32. public DynamicGridColumnNameSelectorGrid(string[] columnNames)
  33. {
  34. var items = new List<DynamicGridColumnNameSelectorItem>();
  35. var parentCols = new HashSet<string>();
  36. foreach (var column in columnNames)
  37. {
  38. var props = column.Split('.');
  39. string? parent = null;
  40. for (int i = 0; i < props.Length - 1; ++i)
  41. {
  42. if (parent is null)
  43. {
  44. parent = props[i];
  45. }
  46. else
  47. {
  48. parent = $"{parent}.{props[i]}";
  49. }
  50. parentCols.Add(parent);
  51. }
  52. var item = new DynamicGridColumnNameSelectorItem
  53. {
  54. ColumnName = column,
  55. ParentColumn = parent,
  56. Display = props[^1],
  57. IsParent = false
  58. };
  59. items.Add(item);
  60. }
  61. foreach (var col in parentCols)
  62. {
  63. var lastColIdx = col.LastIndexOf('.');
  64. var item = new DynamicGridColumnNameSelectorItem
  65. {
  66. ColumnName = col,
  67. IsParent = true
  68. };
  69. if (lastColIdx == -1)
  70. {
  71. item.ParentColumn = null;
  72. item.Display = col;
  73. }
  74. else
  75. {
  76. item.ParentColumn = col[..lastColIdx];
  77. item.Display = col[(lastColIdx + 1)..];
  78. }
  79. items.Add(item);
  80. }
  81. items.Sort((a, b) => a.ColumnName.CompareTo(b.ColumnName));
  82. Items = items;
  83. }
  84. protected override void SelectItems(CoreRow[]? rows)
  85. {
  86. base.SelectItems(rows);
  87. CanSave = rows is not null && rows.Any(x => !LoadItem(x).IsParent);
  88. if(rows is not null)
  89. {
  90. foreach(var row in rows)
  91. {
  92. Component.ExpandRow(row);
  93. }
  94. }
  95. }
  96. protected override DynamicGridColumns LoadColumns()
  97. {
  98. var columns = new DynamicGridColumns<DynamicGridColumnNameSelectorItem>();
  99. columns.Add(x => x.Display, caption: "Name");
  100. return columns;
  101. }
  102. private UIComponent? _uiComponent;
  103. private UIComponent Component
  104. {
  105. get
  106. {
  107. _uiComponent ??= new UIComponent(this);
  108. return _uiComponent;
  109. }
  110. }
  111. private class UIComponent : DynamicGridTreeUIComponent<DynamicGridColumnNameSelectorItem, string?>
  112. {
  113. DynamicGridColumnNameSelectorGrid Grid;
  114. public UIComponent(DynamicGridColumnNameSelectorGrid grid) : base(x => x.ColumnName, x => x.ParentColumn, null)
  115. {
  116. Parent = grid;
  117. Grid = grid;
  118. ExpandMode = DynamicTreeGridExpandMode.None;
  119. ShowHeader = false;
  120. MinRowHeight = 25;
  121. }
  122. protected override Brush? GetCellForeground(CoreRow row, DynamicColumnBase column)
  123. {
  124. var item = Grid.LoadItem(row);
  125. if (item.IsParent)
  126. {
  127. return Colors.Gray.ToBrush();
  128. }
  129. else
  130. {
  131. return base.GetCellForeground(row, column);
  132. }
  133. }
  134. protected override FontStyle? GetCellFontStyle(CoreRow row, DynamicColumnBase column)
  135. {
  136. var item = Grid.LoadItem(row);
  137. if (item.IsParent)
  138. {
  139. return FontStyles.Italic;
  140. }
  141. else
  142. {
  143. return base.GetCellFontStyle(row, column);
  144. }
  145. }
  146. }
  147. protected override IDynamicGridUIComponent<DynamicGridColumnNameSelectorItem> CreateUIComponent() => Component;
  148. public event PropertyChangedEventHandler? PropertyChanged;
  149. protected void DoPropertyChanged([CallerMemberName] string propertyName = "")
  150. {
  151. PropertyChanged?.Invoke(this, new(propertyName));
  152. }
  153. public static bool SelectColumnName(string[] columnNames, out string value)
  154. {
  155. var grid = new DynamicGridColumnNameSelectorGrid(columnNames);
  156. grid.Refresh(true, true);
  157. grid.OnCellDoubleClick += (o, e) =>
  158. {
  159. if (e.Row is null) return;
  160. var item = grid.LoadItem(e.Row);
  161. };
  162. var window = new DynamicContentDialog(grid)
  163. {
  164. Title = "Select Column"
  165. };
  166. window.Bind(DynamicContentDialog.CanSaveProperty, grid, x => x.CanSave);
  167. if(window.ShowDialog() == true && grid.SelectedRows.FirstOrDefault() is CoreRow row)
  168. {
  169. var item = grid.LoadItem(row);
  170. if (!item.IsParent)
  171. {
  172. value = item.ColumnName;
  173. return true;
  174. }
  175. }
  176. value = "";
  177. return false;
  178. }
  179. }
  180. public class DynamicGridColumnNameSelectorItem : BaseObject
  181. {
  182. public string ColumnName { get; set; } = "";
  183. public string Display { get; set; } = "";
  184. public string? ParentColumn { get; set; }
  185. public bool IsParent { get; set; }
  186. }