DynamicGridColumnNameSelectorWindow.cs 5.6 KB

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