MultiLookupEditorControl.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using Syncfusion.Windows.Tools.Controls;
  11. using Syncfusion.Data.Extensions;
  12. using NPOI.SS.Formula.Functions;
  13. namespace InABox.DynamicGrid
  14. {
  15. public class MultiLookupEditorControl : DynamicEditorControl<object?, MultiLookupEditor>, ILookupEditorControl
  16. {
  17. static MultiLookupEditorControl()
  18. {
  19. //DynamicEditorControlFactory.Register<MultiLookupEditorControl, MultiLookupEditor>();
  20. }
  21. private ComboBoxAdv Editor;
  22. public MultiLookupEditorControl()
  23. {
  24. OtherColumns = new Dictionary<string, string>();
  25. Lookups = new Dictionary<object, string>();
  26. Width = int.MaxValue;
  27. }
  28. public int Width { get; set; }
  29. public Dictionary<object, string> Lookups { get; set; }
  30. public Dictionary<string, string> OtherColumns { get; }
  31. private List<Tuple<object?, string>> Items;
  32. public ILookupEditor LookupEditorDefinition => EditorDefinition;
  33. public override void Configure()
  34. {
  35. Editor.HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left;
  36. if (!Width.Equals(int.MaxValue))
  37. Editor.Width = Width;
  38. Editor.IsEnabled = false;
  39. Host.LoadLookups(this);
  40. }
  41. public void LoadLookups(CoreTable values)
  42. {
  43. var keycol = ColumnName.Split('.').Last();
  44. //var prefix = string.Join(".", ColumnName.Split('.').Reverse().Skip(1).Reverse());
  45. /*foreach (var col in values.Columns)
  46. if (!string.Equals(col.ColumnName, keycol) && !string.Equals(col.ColumnName, "Display"))
  47. if (!OtherColumns.ContainsKey(col.ColumnName))
  48. OtherColumns[col.ColumnName] = string.IsNullOrWhiteSpace(prefix) ? col.ColumnName : string.Join(".", prefix, col.ColumnName);*/
  49. var value = RetrieveValue();
  50. //Lookups = lookups;
  51. Lookups.Clear();
  52. Items = new List<Tuple<object?, string>>();
  53. //if (!IsEnumEditor())
  54. // Editor.Items.Add("");
  55. var selected = Editor.SelectedItems is not null ? Editor.SelectedItems.Cast<Tuple<object?, string>>().Select(x => x.Item1) : Enumerable.Empty<object?>();
  56. foreach (var row in values.Rows)
  57. {
  58. var key = row[keycol];
  59. Items.Add(new(key, $"{row["Display"]}"));
  60. if (key != null)
  61. {
  62. Lookups[key] = string.Format("{0}", row["Display"]);
  63. }
  64. }
  65. Editor.ItemsSource = Items;
  66. Editor.DisplayMemberPath = "Item2";
  67. Editor.SelectedItems = Items.Where(x => selected.Contains(x.Item1)).ToObservableCollection();
  68. //var sel = values.Rows.FirstOrDefault(r => r[keycol].Equals(value));
  69. //if(sel is null)
  70. //if (IsEnumEditor())
  71. // Editor.SelectedIndex = sel != null ? sel.Index : 0;
  72. //else
  73. // Editor.SelectedIndex = sel != null ? sel.Index + 1 : 0;
  74. //foreach (var key in lookups.Keys)
  75. // Editor.Items.Add(lookups[key]);
  76. //if ((value != null) && Lookups.ContainsKey(value))
  77. // Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  78. Editor.IsEnabled = EditorDefinition.Editable.IsEditable();
  79. }
  80. public override int DesiredHeight()
  81. {
  82. return 25;
  83. }
  84. public override void SetFocus()
  85. {
  86. Editor.Focus();
  87. }
  88. private bool IsEnumEditor()
  89. {
  90. return false;
  91. }
  92. public List<Button> Buttons { get; private set; }
  93. protected override FrameworkElement CreateEditor()
  94. {
  95. var dock = new DockPanel();
  96. Buttons = CreateButtons(out var DisableEditor);
  97. foreach (var button in Buttons)
  98. {
  99. button.SetValue(DockPanel.DockProperty, Dock.Right);
  100. dock.Children.Add(button);
  101. dock.Width += button.Width + 5;
  102. }
  103. Editor = new ComboBoxAdv
  104. {
  105. VerticalAlignment = VerticalAlignment.Stretch,
  106. VerticalContentAlignment = VerticalAlignment.Center,
  107. HorizontalAlignment = Width.Equals(int.MaxValue) ? HorizontalAlignment.Stretch : HorizontalAlignment.Left,
  108. IsEditable = false,
  109. AllowMultiSelect = true
  110. };
  111. Editor.SelectionChanged += Combobox_SelectionChanged;
  112. Editor.IsEnabled = false;
  113. if (DisableEditor)
  114. {
  115. Editor.Background = new SolidColorBrush(Colors.Silver);
  116. Editor.IsEnabled = false;
  117. }
  118. dock.Children.Add(Editor);
  119. return dock;
  120. }
  121. private void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  122. {
  123. if (!Editor.IsEnabled)
  124. return;
  125. /*if (LookupTable != null)
  126. {
  127. var keycol = ColumnName.Split('.').Last();
  128. var row = LookupTable.Rows.FirstOrDefault(r => object.Equals(r[keycol], RetrieveValue()));
  129. foreach (var field in LookupTable.Columns.Where(x => OtherColumns.ContainsKey(x.ColumnName)))
  130. {
  131. var othervalue = row?[field.ColumnName];
  132. OtherValues[OtherColumns[field.ColumnName]] = othervalue;
  133. }
  134. }*/
  135. CheckChanged();
  136. }
  137. public override int DesiredWidth()
  138. {
  139. return int.MaxValue;
  140. }
  141. protected override object? RetrieveValue()
  142. {
  143. if (Editor.SelectedIndex >= 0 && Editor.SelectedIndex < Lookups.Keys.Count)
  144. return string.Join(',', Editor.SelectedItems.Cast<Tuple<object?, string>>().Select(x => x.Item1));
  145. return "";
  146. }
  147. protected override void UpdateValue(object? value)
  148. {
  149. if (Editor == null)
  150. return;
  151. if (value == null)
  152. {
  153. Editor.SelectedItem = null;
  154. return;
  155. }
  156. if (!Loaded && !Editor.IsEnabled)
  157. {
  158. Lookups.Clear();
  159. Lookups[value] = "Loading";
  160. Editor.Items.Clear();
  161. Editor.Items.Add("");
  162. Editor.Items.Add("Loading...");
  163. Editor.SelectedIndex = IsEnumEditor() ? 0 : 1;
  164. return;
  165. }
  166. if(value is not string str)
  167. {
  168. return;
  169. }
  170. IEnumerable<Tuple<object?, string>> selected;
  171. if (!string.IsNullOrWhiteSpace(str))
  172. {
  173. var values = str.Split(',');
  174. selected = values.Select(x => Items.FirstOrDefault(y => y.Item1?.ToString() == x)).Where(x => x is not null)!;
  175. }
  176. else
  177. {
  178. selected = Enumerable.Empty<Tuple<object?, string>>();
  179. }
  180. Editor.SelectedItems = selected.ToObservableCollection();
  181. /*if (Lookups.ContainsKey(value))
  182. {
  183. if (IsEnumEditor())
  184. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value);
  185. else
  186. Editor.SelectedIndex = Lookups.Keys.ToList().IndexOf(value) + 1;
  187. }
  188. else
  189. {
  190. Editor.SelectedIndex = 0;
  191. }*/
  192. }
  193. public override void SetColor(Color color)
  194. {
  195. //Editor.Background = new SolidColorBrush(color);
  196. }
  197. public override void SetEnabled(bool enabled)
  198. {
  199. base.SetEnabled(enabled);
  200. Editor.IsEnabled = enabled;
  201. }
  202. }
  203. }