MultiLookupEditorControl.cs 7.9 KB

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