MultiLookupEditorControl.cs 7.9 KB

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